Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(603)

Side by Side Diff: ios/web/webui/resources/support.js

Issue 2946383002: Support new-style Mojo JS core API on IOS. (Closed)
Patch Set: . Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/web/webui/resources/mojo_api.js ('k') | ios/web/webui/resources/sync_message_channel.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Module "mojo/public/js/support"
6 //
7 // This module provides the JavaScript bindings for mojo/public/c/system/core.h.
8 // Refer to that file for more detailed documentation for equivalent methods.
9
10 define("mojo/public/js/support", [
11 "ios/mojo/public/js/sync_message_channel",
12 "ios/mojo/public/js/handle_util",
13 ], function(syncMessageChannel, handleUtil) {
14
15 /**
16 * Holds callbacks for all currently active watches.
17 * @constructor
18 */
19 var MojoWatchCallbacksHolder = function() {
20 /**
21 * Next callback Id to be used for watch.
22 * @private {number}
23 */
24 this.nextCallbackId_ = 0;
25
26 /**
27 * Map where keys are callbacks ids and values are callbacks.
28 * @private {!Map}
29 */
30 this.callbacks_ = new Map();
31
32 /**
33 * Map where keys are watch ids and values are callback ids.
34 * @private {!Map}
35 */
36 this.callbackIds_ = new Map();
37 };
38
39
40 MojoWatchCallbacksHolder.prototype = {
41 /**
42 * Calls watch callback.
43 *
44 * @param {number} callbackId callback id previously returned from
45 {@code getNextCallbackId}.
46 * @param {!MojoResult} mojoResult to call callback with.
47 */
48 callCallback: function(callbackId, mojoResult) {
49 var callback = this.callbackIds_.get(callbackId);
50
51 // Signalling the watch is asynchronous operation and this function may be
52 // called for already removed watch.
53 if (callback)
54 callback(mojoResult);
55 },
56
57 /**
58 * Returns next callback Id to be used for watch (idempotent).
59 *
60 * @return {number} callback id.
61 */
62 getNextCallbackId: function() {
63 return this.nextCallbackId_;
64 },
65
66 /**
67 * Adds callback which must be executed when the Watch fires.
68 *
69 * @param {!MojoWatchId} watchId returned from "support.watch" mojo
70 backend.
71 * @param {!function (mojoResult)} callback which should be executed when
72 the Watch fires.
73 */
74 addWatchCallback: function (watchId, callback) {
75 this.callbackIds_.set(watchId, this.nextCallbackId_);
76 this.callbackIds_.set(this.nextCallbackId_, callback);
77 ++this.nextCallbackId_;
78 },
79
80 /**
81 * Removes callback which should no longer be executed.
82 *
83 * @param {!MojoWatchId} watchId to remove callback for.
84 */
85 removeWatchCallback: function (watchId) {
86 this.callbacks_.delete(this.callbackIds_.get(watchId));
87 this.callbackIds_.delete(watchId);
88 },
89 };
90
91 var watchCallbacksHolder = new MojoWatchCallbacksHolder();
92
93
94 /**
95 * Public functions called by Mojo backend when watch callback fires.
96 * @public {!function(callbackId, mojoResult)}
97 */
98 __crWeb.mojo = {};
99 __crWeb.mojo.signalWatch = function(callbackId, mojoResult) {
100 watchCallbacksHolder.callCallback(callbackId, mojoResult);
101 }
102
103 /*
104 * Begins watching a handle for |signals| to be satisfied or unsatisfiable.
105 *
106 * @param {!MojoHandle} handle The handle to watch.
107 * @param {!MojoHandleSignals} signals The signals to watch.
108 * @param {!function (mojoResult)} calback Called with a result any time
109 * the watched signals become satisfied or unsatisfiable.
110 *
111 * @return {!MojoWatchId} watchId An opaque identifier that identifies this
112 * watch.
113 */
114 function watch(handle, signals, callback) {
115 var watchId = syncMessageChannel.sendMessage({
116 name: "support.watch",
117 args: {
118 handle: handleUtil.getNativeHandle(handle),
119 signals: signals,
120 callbackId: watchCallbacksHolder.getNextCallbackId()
121 }
122 });
123 watchCallbacksHolder.addWatchCallback(watchId, callback);
124 return watchId;
125 }
126
127 /*
128 * Cancels a handle watch initiated by watch().
129 *
130 * @param {!MojoWatchId} watchId The watch identifier returned by watch().
131 */
132 function cancelWatch(watchId) {
133 syncMessageChannel.sendMessage({
134 name: "support.cancelWatch",
135 args: {
136 watchId: watchId
137 }
138 });
139 watchCallbacksHolder.removeWatchCallback(watchId);
140 }
141
142 var exports = {};
143 exports.watch = watch;
144 exports.cancelWatch = cancelWatch;
145 return exports;
146 });
OLDNEW
« no previous file with comments | « ios/web/webui/resources/mojo_api.js ('k') | ios/web/webui/resources/sync_message_channel.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698