Skip to content

Commit df0ab3e

Browse files
Merge pull request #2 from PubNubDevelopers/Access_Manager
Access manager
2 parents cb12c13 + da44103 commit df0ab3e

40 files changed

+108283
-24
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Files and directories created by pub
44
.dart_tool/
55
.packages
6-
build/
76
# If you're building an application, you may want to check-in your pubspec.lock
87
pubspec.lock
98

@@ -12,10 +11,7 @@ pubspec.lock
1211
doc/api/
1312

1413
# Avoid committing generated Javascript files:
15-
*.dart.js
1614
*.info.json # Produced by the --dump-info flag.
17-
*.js # When generated by dart2js. Don't specify *.js if your
18-
# project includes source files written in JavaScript.
1915
*.js_
2016
*.js.deps
2117
*.js.map

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ cd flutter_sdk_tutorial
7676
flutter packages get
7777
```
7878

79-
- Add your pub/sub keys to `app_state.dart` which can be found in the `\lib\utils` directory.
79+
- Add your pub/sub keys to `keys.dart` which can be found in the `\lib\utils` directory.
8080

8181
- Run the application in Chrome: `flutter run -d chrome`. You can also run the app with any target device you have configured Flutter for, for example an Android emulator or iPhone simulator.
8282

flutter_sdk_tutorial/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ migrate_working_dir/
3030
.packages
3131
.pub-cache/
3232
.pub/
33-
/build/
3433

3534
# Web related
3635
lib/generated_plugin_registrant.dart
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"inputs":[],"outputs":[]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"inputs":[],"outputs":[]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"inputs":[],"outputs":[]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"packages/cupertino_icons/assets/CupertinoIcons.ttf":["packages/cupertino_icons/assets/CupertinoIcons.ttf"]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"family":"MaterialIcons","fonts":[{"asset":"fonts/MaterialIcons-Regular.otf"}]},{"family":"packages/cupertino_icons/CupertinoIcons","fonts":[{"asset":"packages/cupertino_icons/assets/CupertinoIcons.ttf"}]}]

flutter_sdk_tutorial/build/flutter_assets/NOTICES

Lines changed: 17041 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c3081ada40b8aad21981c9b91bfc51f1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"packages/cupertino_icons/assets/CupertinoIcons.ttf":["packages/cupertino_icons/assets/CupertinoIcons.ttf"]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"family":"MaterialIcons","fonts":[{"asset":"fonts/MaterialIcons-Regular.otf"}]},{"family":"packages/cupertino_icons/CupertinoIcons","fonts":[{"asset":"packages/cupertino_icons/assets/CupertinoIcons.ttf"}]}]

flutter_sdk_tutorial/build/web/assets/NOTICES

Lines changed: 17041 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.

flutter_sdk_tutorial/build/web/canvaskit/canvaskit.js

Lines changed: 281 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

flutter_sdk_tutorial/build/web/canvaskit/profiling/canvaskit.js

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
917 Bytes
Loading
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"features": [
3+
"Send a message",
4+
"Receive a message (You might need to open a new tab)",
5+
"Change your friendly name",
6+
"Be in a chat with 3 or more people"
7+
]
8+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// Copyright 2014 The Flutter 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+
/**
6+
* This script installs service_worker.js to provide PWA functionality to
7+
* application. For more information, see:
8+
* https://developers.google.com/web/fundamentals/primers/service-workers
9+
*/
10+
11+
if (!_flutter) {
12+
var _flutter = {};
13+
}
14+
_flutter.loader = null;
15+
16+
(function() {
17+
"use strict";
18+
class FlutterLoader {
19+
/**
20+
* Creates a FlutterLoader, and initializes its instance methods.
21+
*/
22+
constructor() {
23+
// TODO: Move the below methods to "#private" once supported by all the browsers
24+
// we support. In the meantime, we use the "revealing module" pattern.
25+
26+
// Watchdog to prevent injecting the main entrypoint multiple times.
27+
this._scriptLoaded = null;
28+
29+
// Resolver for the pending promise returned by loadEntrypoint.
30+
this._didCreateEngineInitializerResolve = null;
31+
32+
// Called by Flutter web.
33+
// Bound to `this` now, so "this" is preserved across JS <-> Flutter jumps.
34+
this.didCreateEngineInitializer = this._didCreateEngineInitializer.bind(this);
35+
}
36+
37+
/**
38+
* Initializes the main.dart.js with/without serviceWorker.
39+
* @param {*} options
40+
* @returns a Promise that will eventually resolve with an EngineInitializer,
41+
* or will be rejected with the error caused by the loader.
42+
*/
43+
loadEntrypoint(options) {
44+
const {
45+
entrypointUrl = "main.dart.js",
46+
serviceWorker,
47+
} = (options || {});
48+
return this._loadWithServiceWorker(entrypointUrl, serviceWorker);
49+
}
50+
51+
/**
52+
* Resolves the promise created by loadEntrypoint.
53+
* Called by Flutter through the public `didCreateEngineInitializer` method,
54+
* which is bound to the correct instance of the FlutterLoader on the page.
55+
* @param {*} engineInitializer
56+
*/
57+
_didCreateEngineInitializer(engineInitializer) {
58+
if (typeof this._didCreateEngineInitializerResolve != "function") {
59+
console.warn("Do not call didCreateEngineInitializer by hand. Start with loadEntrypoint instead.");
60+
}
61+
this._didCreateEngineInitializerResolve(engineInitializer);
62+
// Remove the public method after it's done, so Flutter Web can hot restart.
63+
delete this.didCreateEngineInitializer;
64+
}
65+
66+
_loadEntrypoint(entrypointUrl) {
67+
if (!this._scriptLoaded) {
68+
this._scriptLoaded = new Promise((resolve, reject) => {
69+
let scriptTag = document.createElement("script");
70+
scriptTag.src = entrypointUrl;
71+
scriptTag.type = "application/javascript";
72+
// Cache the resolve, so it can be called from Flutter.
73+
// Note: Flutter hot restart doesn't re-create this promise, so this
74+
// can only be called once. Instead, we need to model this as a stream
75+
// of `engineCreated` events coming from Flutter that are handled by JS.
76+
this._didCreateEngineInitializerResolve = resolve;
77+
scriptTag.addEventListener("error", reject);
78+
document.body.append(scriptTag);
79+
});
80+
}
81+
82+
return this._scriptLoaded;
83+
}
84+
85+
_waitForServiceWorkerActivation(serviceWorker, entrypointUrl) {
86+
if (!serviceWorker || serviceWorker.state == "activated") {
87+
if (!serviceWorker) {
88+
console.warn("Cannot activate a null service worker. Falling back to plain <script> tag.");
89+
} else {
90+
console.debug("Service worker already active.");
91+
}
92+
return this._loadEntrypoint(entrypointUrl);
93+
}
94+
return new Promise((resolve, _) => {
95+
serviceWorker.addEventListener("statechange", () => {
96+
if (serviceWorker.state == "activated") {
97+
console.debug("Installed new service worker.");
98+
resolve(this._loadEntrypoint(entrypointUrl));
99+
}
100+
});
101+
});
102+
}
103+
104+
_loadWithServiceWorker(entrypointUrl, serviceWorkerOptions) {
105+
if (!("serviceWorker" in navigator) || serviceWorkerOptions == null) {
106+
console.warn("Service worker not supported (or configured). Falling back to plain <script> tag.", serviceWorkerOptions);
107+
return this._loadEntrypoint(entrypointUrl);
108+
}
109+
110+
const {
111+
serviceWorkerVersion,
112+
timeoutMillis = 4000,
113+
} = serviceWorkerOptions;
114+
115+
let serviceWorkerUrl = "flutter_service_worker.js?v=" + serviceWorkerVersion;
116+
let loader = navigator.serviceWorker.register(serviceWorkerUrl)
117+
.then((reg) => {
118+
if (!reg.active && (reg.installing || reg.waiting)) {
119+
// No active web worker and we have installed or are installing
120+
// one for the first time. Simply wait for it to activate.
121+
let sw = reg.installing || reg.waiting;
122+
return this._waitForServiceWorkerActivation(sw, entrypointUrl);
123+
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
124+
// When the app updates the serviceWorkerVersion changes, so we
125+
// need to ask the service worker to update.
126+
console.debug("New service worker available.");
127+
return reg.update().then((reg) => {
128+
console.debug("Service worker updated.");
129+
let sw = reg.installing || reg.waiting || reg.active;
130+
return this._waitForServiceWorkerActivation(sw, entrypointUrl);
131+
});
132+
} else {
133+
// Existing service worker is still good.
134+
console.debug("Loading app from service worker.");
135+
return this._loadEntrypoint(entrypointUrl);
136+
}
137+
});
138+
139+
// Timeout race promise
140+
let timeout;
141+
if (timeoutMillis > 0) {
142+
timeout = new Promise((resolve, _) => {
143+
setTimeout(() => {
144+
if (!this._scriptLoaded) {
145+
console.warn("Failed to load app from service worker. Falling back to plain <script> tag.");
146+
resolve(this._loadEntrypoint(entrypointUrl));
147+
}
148+
}, timeoutMillis);
149+
});
150+
}
151+
152+
return Promise.race([loader, timeout]);
153+
}
154+
}
155+
156+
_flutter.loader = new FlutterLoader();
157+
}());

0 commit comments

Comments
 (0)