forked from oraoto/pib
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathPhpWorker.js
76 lines (60 loc) · 1.53 KB
/
PhpWorker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { PhpBase } from './PhpBase';
import PhpBinary from './php-worker';
import { commitTransaction, startTransaction } from './webTransactions';
export class PhpWorker extends PhpBase
{
constructor(args = {})
{
super(PhpBinary, args);
}
startTransaction()
{
return startTransaction(this);
}
commitTransaction(readOnly = false)
{
return commitTransaction(this, readOnly);
}
async refresh()
{
super.refresh();
if(!this.phpArgs.persist)
{
return;
}
const php = await this.binary;
await navigator.locks.request('php-wasm-fs-lock', () => {
new Promise((accept, reject) => {
php.FS.syncfs(true, error => {
if(error) reject(error);
else accept();
});
});
});
}
async _enqueue(callback, params = [], readOnly = false)
{
await this.binary;
let accept, reject;
const coordinator = new Promise((a,r) => [accept, reject] = [a, r]);
const _accept = result => accept(result);
const _reject = reason => reject(reason);
this.queue.push([callback, params, _accept, _reject]);
navigator.locks.request('php-wasm-fs-lock', async () => {
if(!this.queue.length)
{
return;
}
await ((this.autoTransaction && !readOnly) ? this.startTransaction() : Promise.resolve());
do
{
const [callback, params, accept, reject] = this.queue.shift();
const run = callback(...params);
run.then(accept).catch(reject);
await run;
} while(this.queue.length)
await (this.autoTransaction ? this.commitTransaction(readOnly) : Promise.resolve());
});
return coordinator;
}
}