aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/data/context.js
diff options
context:
space:
mode:
authorValerio Virgillito2012-06-17 22:31:44 -0700
committerValerio Virgillito2012-06-17 22:31:44 -0700
commit8fe92b94ce5e1e2857d088752d94e19db7e3d8a8 (patch)
treed84807aae0b974b5200050972dd94da6066e363b /node_modules/montage/data/context.js
parente570fc8518cf03dd03c15982edcf17c5ba0a293d (diff)
downloadninja-8fe92b94ce5e1e2857d088752d94e19db7e3d8a8.tar.gz
montage v11 merge into ninja
Signed-off-by: Valerio Virgillito <valerio@motorola.com>
Diffstat (limited to 'node_modules/montage/data/context.js')
-rwxr-xr-xnode_modules/montage/data/context.js375
1 files changed, 0 insertions, 375 deletions
diff --git a/node_modules/montage/data/context.js b/node_modules/montage/data/context.js
deleted file mode 100755
index ca992ff0..00000000
--- a/node_modules/montage/data/context.js
+++ /dev/null
@@ -1,375 +0,0 @@
1/* <copyright>
2 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4 (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5 </copyright> */
6/**
7 @module montage/data/context
8 @requires montage/core/core
9 @requires montage/data/store
10 @requires montage/data/blueprint
11 @requires montage/data/objectproperty
12 @requires montage/core/shim/weak-map
13 @requires montage/core/shim/structures
14 @requires montage/core/exception
15 @requires montage/core/promise
16 @requires montage/core/logger
17 */
18var Montage = require("montage").Montage;
19var Store = require("data/store").Store;
20var Blueprint = require("data/blueprint").Blueprint;
21var ObjectProperty = require("data/objectproperty").ObjectProperty;
22// TODO [June 5 2011 PJYF] This s temporary implementation of WeakMap to let the browser catch up.
23var WeakMap = require("core/shim/weak-map").WeakMap;
24var Set = require("core/shim/structures").Set;
25var Exception = require("core/exception").Exception;
26var Promise = require("core/promise").Promise;
27var logger = require("core/logger").logger("context");
28/**
29 @class module:montage/data/context.Context
30 @extends module:montage/data/store.Store
31 */
32var Context = exports.Context = Montage.create(Store, /** @lends module:montage/data/context.Context# */ {
33 /**
34 Collection of object inserted in this context since the last save.
35 @private
36 */
37 _inserted: {
38 value: new Set(50),
39 serializable: true,
40 distinct: true,
41 enumerable: false,
42 writable: false
43 },
44 /**
45 Collection of object deleted in this context since the last save.
46 @private
47 */
48 _deleted: {
49 value: new Set(50),
50 serializable: true,
51 distinct: true,
52 enumerable: false,
53 writable: false
54 },
55 /**
56 Collection of object modified in this context since the last save.
57 @private
58 */
59 _modified: {
60 value: new Set(50),
61 serializable: true,
62 distinct: true,
63 enumerable: false,
64 writable: false
65 },
66
67 /**
68 Table of fetched objects for uniquing. The key is the object ID the value the actual object or the pledge representing it.<br/>
69 <b>Note:<b/> This is a weak map so that the context does not hold on the objects and they can be garbage collected if no one else hold on them.
70 @private
71 */
72 _objectMap: {
73 value: new WeakMap(),
74 serializable: true,
75 enumerable: false,
76 writable: false
77 },
78
79 /**
80 Collection of object inserted in this context since the last save.
81 @function
82 @returns this._inserted
83 @default empty set
84 */
85 inserted: {
86 get: function() {
87 return this._inserted;
88 }
89 },
90
91 /**
92 Collection of object deleted in this context since the last save.
93 @function
94 @returns this._deleted
95 @default empty set
96 */
97 deleted: {
98 get: function() {
99 return this._deleted;
100 }
101 },
102
103 /**
104 Collection of object modified in this context since the last save.
105 @function
106 @returns this._modified
107 @default empty set
108 */
109 modified: {
110 get: function() {
111 return this._modified;
112 }
113 },
114
115 /**
116 Description TODO
117 @function
118 @param {String} id objectmap
119 @returns this._objectMap.get(id) | null
120 */
121 objectForId: {
122 value: function(id) {
123 if (this._objectMap.has(id)) {
124 return this._objectMap.get(id);
125 }
126 return null;
127 }
128 },
129
130 /**
131 Inserts a newly created object in the context.
132 @function
133 @param {Object} instance TODO
134 @returns initialized object
135 */
136 insert: {
137 value: function(instance) {
138 if (instance !== null) {
139 if (typeof instance.context === "undefined") {
140 var metadata = Montage.getInfoForObject(instance);
141 var blueprint = this.blueprintForPrototype(metadata.objectName, metadata.moduleId);
142 if (blueprint !== null) {
143 ObjectProperty.manager.apply(Object.getPrototypeOf(instance), blueprint);
144 } else {
145 throw Exception.create().initWithMessageTargetAndMethod("Cannot find blueprint for: " + metadata.objectName + " " + metadata.moduleId, this, "insert");
146 }
147 }
148 if (instance.context === null) {
149 instance.context = this;
150 this._inserted.add(instance);
151 var self = this;
152 return this.initializeObject(instance, this).then(function(instance) {
153 self._objectMap.set(instance.objectId, instance);
154 return Promise.ref(instance);
155 });
156 } else if (instance.context !== this) {
157 throw Exception.initWithMessageTargetAndMethod("This instance is already inserted in another context.", this, "insert");
158 }
159 } else {
160 throw Exception.initWithMessageTargetAndMethod("Cannot insert a null object.", this, "insert");
161 }
162 }
163 },
164
165 /**
166 Delete an object.<br>
167 A deleted object will be deleted from the backing store on the next save.
168 @function
169 @param {Object} instance TODO
170 @returns Promise.ref(instance)
171 */
172 'delete': {
173 value: function(instance) {
174 if (instance !== null) {
175 if ((typeof instance.context === "undefined") || (instance.context === null)) {
176 return Promise.ref(instance);
177 }
178 if (instance.context !== this) {
179 throw Exception.initWithMessageTargetAndMethod("This instance is belongs to another context.", this, "delete");
180 }
181 if (this._inserted.has(instance)) {
182 // We are forgetting a newly inserted object
183 this._inserted.delete(instance);
184 if (typeof instance.context !== "undefined") {
185 instance.context = null;
186 }
187 } else {
188 if (this._modified.has(instance)) {
189 // the object was modified before teh delete forget those.
190 this._modified.delete(instance);
191 instance = this._revertValues(instance);
192 }
193 this._deleted.add(instance);
194 }
195 this._objectMap.delete(instance.objectId);
196 } else {
197 throw Exception.initWithMessageTargetAndMethod("Cannot delete a null object.", this, "delete");
198 }
199 return Promise.ref(instance);
200 }
201 },
202
203 /**
204 Revert an object to its saved values.
205 @function
206 @param {Object} instance TODO
207 @returns Promise.ref(instance)
208 */
209 revert: {
210 value: function(instance) {
211 if (instance !== null) {
212 if (typeof instance.context === "undefined") {
213 return Promise.ref(instance);
214 }
215 if (instance.context !== null) {
216 if (instance.context !== this) {
217 throw Exception.initWithMessageTargetAndMethod("This instance is belongs to another context.", this, "revert");
218 }
219 if (this._inserted.has(instance)) {
220 // This is a newly inserted object, there is no value to revert to, so do nothing.
221 } else if (this._modified.has(instance)) {
222 this._modified.delete(instance);
223 instance = this._revertValues(instance);
224 }
225 } else {
226 // Maybe that object was deleted let retrieve it?
227 if (this._