alchemymvc 1.3.9 → 1.3.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -732,16 +732,16 @@ Document.setMethod(function setDataRecord(record, options) {
732
732
  });
733
733
 
734
734
  /**
735
- * Get the translated value of a certain field.
736
- * If this document has already been translated, a new instance will be queried
735
+ * Get the translated document for the given prefix with only the given field
737
736
  *
738
737
  * @author Jelle De Loecker <jelle@elevenways.be>
739
- * @since 1.3.0
740
- * @version 1.3.0
738
+ * @since 1.3.10
739
+ * @version 1.3.10
741
740
  *
741
+ * @param {String} field_name
742
742
  * @param {String} prefix
743
743
  */
744
- Document.setMethod(async function getTranslatedValueOfField(field_name, prefix) {
744
+ Document.setMethod(async function getTranslatedDocumentOfPrefix(field_name, prefix) {
745
745
 
746
746
  const model = this.$model;
747
747
 
@@ -752,6 +752,36 @@ Document.setMethod(async function getTranslatedValueOfField(field_name, prefix)
752
752
 
753
753
  let doc = await model.find('first', crit);
754
754
 
755
+ return doc;
756
+ });
757
+
758
+ /**
759
+ * Get the translated value of a certain field.
760
+ * If this document has already been translated, a new instance will be queried
761
+ *
762
+ * @author Jelle De Loecker <jelle@elevenways.be>
763
+ * @since 1.3.0
764
+ * @version 1.3.10
765
+ *
766
+ * @param {String} prefix
767
+ */
768
+ Document.setMethod(async function getTranslatedValueOfField(field_name, prefix) {
769
+ let doc = await this.getTranslatedDocumentOfPrefix(field_name, prefix);
770
+ return doc?.[field_name];
771
+ });
772
+
773
+ /**
774
+ * Get the translated value of a certain field.
775
+ * If this document has already been translated, a new instance will be queried
776
+ *
777
+ * @author Jelle De Loecker <jelle@elevenways.be>
778
+ * @since 1.3.10
779
+ * @version 1.3.10
780
+ *
781
+ * @param {String} prefix
782
+ */
783
+ Document.setMethod(async function getTranslatedValueOfFieldForRoute(field_name, prefix) {
784
+ let doc = await this.getTranslatedDocumentOfPrefix(field_name, prefix);
755
785
  return doc?.[field_name];
756
786
  });
757
787
 
package/lib/app/routes.js CHANGED
@@ -6,4 +6,6 @@ Router.POSTPONED_ROUTE = Router.add({
6
6
  paths : '/alchemy/postponed/{id}',
7
7
  methods : ['get'],
8
8
  can_be_postponed : false,
9
- });
9
+ });
10
+
11
+ Router.linkup('Syncable#linkup', 'syncablelink', 'AlchemyInfo#syncable');
@@ -114,55 +114,60 @@ Conduit.prepareProperty(function renderer() {
114
114
  return result;
115
115
  });
116
116
 
117
+ /**
118
+ * Get the SessionScene instance
119
+ *
120
+ * @author Jelle De Loecker <jelle@elevenways.be>
121
+ * @since 1.3.10
122
+ * @version 1.3.10
123
+ */
124
+ Conduit.setProperty(function scene() {
125
+ return this.getSession().getScene(this.scene_id);
126
+ });
127
+
117
128
  /**
118
129
  * Enforce the scene_id
119
130
  *
120
- * @author Jelle De Loecker <jelle@develry.be>
131
+ * @author Jelle De Loecker <jelle@elevenways.be>
121
132
  * @since 1.1.0
122
- * @version 1.1.7
133
+ * @version 1.3.10
123
134
  */
124
135
  Conduit.enforceProperty(function scene_id(new_value, old_value) {
125
136
 
126
- if (new_value) {
127
- return new_value;
128
- }
129
-
130
- if (this.headers['x-scene-id']) {
131
- return this.headers['x-scene-id'];
132
- }
137
+ if (!new_value) {
138
+ new_value = this.headers['x-scene-id'] || this.expose('scene_id');
133
139
 
134
- // If there also was no old value, create a new scene
135
- if (old_value == null) {
136
- // Generate the scene_id
137
- new_value = Crypto.randomHex(8) || Crypto.pseudoHex(8);
140
+ // If there also was no old value, create a new scene
141
+ if (!new_value && old_value == null) {
142
+ // Generate the scene_id
143
+ new_value = Crypto.randomHex(8) || Crypto.pseudoHex(8);
138
144
 
139
- // Tell the session this scene can be expected
140
- this.getSession().expectScene(new_value);
145
+ // Tell the session this scene can be expected
146
+ this.getSession().expectScene(new_value, this);
141
147
 
142
- let path;
148
+ let path = this.request?.url;
143
149
 
144
- if (this.url) {
145
- path = this.url.path;
146
- }
150
+ // Set the sceneid cookie
151
+ this.cookie('scene_start_' + ~~(Math.random()*1000), {
147
152
 
148
- // Set the sceneid cookie
149
- this.cookie('scene_start_' + ~~(Math.random()*1000), {
153
+ // The time this scene has started
154
+ start: Date.now(),
150
155
 
151
- // The time this scene has started
152
- start: Date.now(),
156
+ // The id of the scene
157
+ id: new_value
158
+ }, {
159
+ // Cookie should only be visible on this path
160
+ path: path,
153
161
 
154
- // The id of the scene
155
- id: new_value
156
- }, {
157
- // Cookie should only be visible on this path
158
- path: path,
162
+ // Cookie should not live for more than 15 seconds
163
+ maxAge: 1000 * 15
164
+ });
165
+ }
166
+ }
159
167
 
160
- // Cookie should not live for more than 15 seconds
161
- maxAge: 1000 * 15
162
- });
168
+ this.expose('scene_id', new_value);
163
169
 
164
- return new_value;
165
- }
170
+ return new_value;
166
171
  });
167
172
 
168
173
  /**
@@ -1819,12 +1824,14 @@ Conduit.setMethod(function end(message) {
1819
1824
  *
1820
1825
  * @author Jelle De Loecker <jelle@elevenways.be>
1821
1826
  * @since 0.2.0
1822
- * @version 1.3.1
1827
+ * @version 1.3.10
1823
1828
  */
1824
1829
  Conduit.setMethod(function _end(message, encoding = 'utf-8') {
1825
1830
 
1826
1831
  this.ended = new Date();
1827
1832
 
1833
+ this.emit('ending');
1834
+
1828
1835
  if (!this.response) {
1829
1836
  let args = [];
1830
1837
 
@@ -809,7 +809,7 @@ Route.setMethod(function getRouteTranslations(controller, conduit) {
809
809
  *
810
810
  * @author Jelle De Loecker <jelle@elevenways.be>
811
811
  * @since 1.3.0
812
- * @version 1.3.0
812
+ * @version 1.3.10
813
813
  *
814
814
  * @param {Controller} controller
815
815
  * @param {Conduit} conduit
@@ -832,8 +832,8 @@ Route.setMethod(async function _getRouteTranslations(controller, conduit, paths)
832
832
  let current_value = conduit.param(param_def.name),
833
833
  new_value;
834
834
 
835
- if (current_value && current_value.getTranslatedValueOfField) {
836
- new_value = await current_value.getTranslatedValueOfField(param_def.name, path.prefix);
835
+ if (current_value && current_value.getTranslatedValueOfFieldForRoute) {
836
+ new_value = await current_value.getTranslatedValueOfFieldForRoute(param_def.name, path.prefix);
837
837
  }
838
838
 
839
839
  if (!new_value) {
@@ -336,17 +336,26 @@ Session.setMethod(function whenConnected(callback) {
336
336
  }
337
337
  });
338
338
 
339
+ /**
340
+ * Get a scene by its id
341
+ *
342
+ * @author Jelle De Loecker <jelle@elevenways.be>
343
+ * @since 1.3.10
344
+ * @version 1.3.10
345
+ */
346
+ Session.setMethod(function getScene(scene_id) {
347
+ return this.expected[scene_id];
348
+ });
349
+
339
350
  /**
340
351
  * Expect the given scene_id to make a connection later on
341
352
  *
342
353
  * @author Jelle De Loecker <jelle@develry.be>
343
354
  * @since 0.2.0
344
- * @version 0.2.0
355
+ * @version 1.3.10
345
356
  */
346
- Session.setMethod(function expectScene(scene_id) {
347
- this.expected[scene_id] = {
348
- updates: [] // Cached updates
349
- };
357
+ Session.setMethod(function expectScene(scene_id, conduit) {
358
+ this.expected[scene_id] = new Classes.Alchemy.SessionScene(conduit, scene_id);
350
359
  });
351
360
 
352
361
  /**
@@ -354,7 +363,7 @@ Session.setMethod(function expectScene(scene_id) {
354
363
  *
355
364
  * @author Jelle De Loecker <jelle@develry.be>
356
365
  * @since 0.2.0
357
- * @version 0.2.0
366
+ * @version 1.3.10
358
367
  */
359
368
  Session.setMethod(function registerConnection(conduit) {
360
369
 
@@ -377,6 +386,10 @@ Session.setMethod(function registerConnection(conduit) {
377
386
  // See if there are any queued updates
378
387
  expected = this.expected[scene_id];
379
388
 
389
+ if (expected) {
390
+ expected.registerConnection(conduit);
391
+ }
392
+
380
393
  if (expected && expected.updates.length) {
381
394
  for (i = 0; i < expected.updates.length; i++) {
382
395
  entry = expected.updates[i];
@@ -404,8 +417,11 @@ Session.setMethod(function registerConnection(conduit) {
404
417
  }
405
418
  }
406
419
 
407
- // Delete the scene from the connections
408
- delete that.connections[scene_id];
420
+ let scene = that.getScene(scene_id);
421
+
422
+ if (scene && !scene.connection_count) {
423
+ scene.destroy();
424
+ }
409
425
  });
410
426
  });
411
427
 
@@ -0,0 +1,95 @@
1
+ /**
2
+ * The Session Scene class:
3
+ * represents a specific scene/tab
4
+ *
5
+ * @author Jelle De Loecker <jelle@elevenways.be>
6
+ * @since 1.3.10
7
+ * @version 1.3.10
8
+ *
9
+ * @param {Conduit} conduit The initializing conduit
10
+ */
11
+ const SessionScene = Function.inherits('Alchemy.Base', function SessionScene(conduit, scene_id) {
12
+
13
+ // Register the creation date
14
+ this.created = new Date();
15
+
16
+ // The scene id
17
+ this.id = scene_id;
18
+
19
+ // The conduit that created this scene
20
+ this.conduit = conduit;
21
+
22
+ // All the websocket connections
23
+ this.connections = [];
24
+
25
+ // Old-style live binding updates.
26
+ this.updates = [];
27
+ });
28
+
29
+ /**
30
+ * The edit action
31
+ *
32
+ * @author Jelle De Loecker <jelle@elevenways.be>
33
+ * @since 1.3.10
34
+ * @version 1.3.10
35
+ *
36
+ * @param {Conduit} conduit
37
+ */
38
+ SessionScene.setMethod(function registerConnection(conduit) {
39
+
40
+ if (!conduit) {
41
+ return;
42
+ }
43
+
44
+ // Add the conduit to the list of connections
45
+ this.connections.push(conduit);
46
+
47
+ this.emit('connected');
48
+
49
+ // When the conduit is closed, remove it from the list
50
+ conduit.on('disconnect', () => {
51
+ this.removeConnection(conduit);
52
+ });
53
+ });
54
+
55
+ /**
56
+ * Remove a connection
57
+ *
58
+ * @author Jelle De Loecker <jelle@elevenways.be>
59
+ * @since 1.3.10
60
+ * @version 1.3.10
61
+ *
62
+ * @param {Conduit} conduit
63
+ */
64
+ SessionScene.setMethod(function removeConnection(conduit) {
65
+
66
+ if (conduit) {
67
+ let index = this.connections.indexOf(conduit);
68
+
69
+ if (index > -1) {
70
+ this.connections.splice(index, 1);
71
+ }
72
+ }
73
+
74
+ if (this.connections.length == 0) {
75
+ this.unsee('connected');
76
+ }
77
+ });
78
+
79
+ /**
80
+ * Destroy this scene
81
+ *
82
+ * @author Jelle De Loecker <jelle@elevenways.be>
83
+ * @since 1.3.10
84
+ * @version 1.3.10
85
+ */
86
+ SessionScene.setMethod(function destroy() {
87
+
88
+ let session = this.conduit.getSession();
89
+
90
+ if (session) {
91
+ delete session.expected[this.id];
92
+ }
93
+
94
+ this.emit('destroyed');
95
+ });
@@ -590,6 +590,8 @@ if (Blast.isBrowser) {
590
590
  */
591
591
  Alchemy.setMethod(function connect(callback) {
592
592
 
593
+ this._connecting_to_server = true;
594
+
593
595
  if (typeof window.io === 'undefined') {
594
596
  hawkejs.require(['socket.io.js', 'socket.io-stream'], function() {
595
597
  alchemy.server.connect(callback);
@@ -1009,11 +1011,16 @@ Alchemy.setMethod(function sendBindingRequests() {
1009
1011
  *
1010
1012
  * @author Jelle De Loecker <jelle@develry.be>
1011
1013
  * @since 1.0.5
1012
- * @version 1.0.5
1014
+ * @version 1.3.10
1013
1015
  */
1014
1016
  Alchemy.setMethod(function enableWebsockets() {
1015
1017
 
1016
- var that = this;
1018
+ // Don't connect twice
1019
+ if (this._connecting_to_server) {
1020
+ return;
1021
+ }
1022
+
1023
+ let that = this;
1017
1024
 
1018
1025
  // Connect to the server
1019
1026
  this.connect(function onConnect() {
@@ -1054,8 +1061,6 @@ Alchemy.setMethod(function enableWebsockets() {
1054
1061
  data,
1055
1062
  i;
1056
1063
 
1057
- console.log('Sending to server...', e);
1058
-
1059
1064
  if (this.hasAttribute('data-server-event-form')) {
1060
1065
  get_form = this.getAttribute('data-server-event-form');
1061
1066
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "alchemymvc",
3
3
  "description": "MVC framework for Node.js",
4
- "version": "1.3.9",
4
+ "version": "1.3.10",
5
5
  "author": "Jelle De Loecker <jelle@elevenways.be>",
6
6
  "keywords": [
7
7
  "alchemy",