Skip to content

Commit b53b5ab

Browse files
Initial commit
0 parents  commit b53b5ab

File tree

6 files changed

+304
-0
lines changed

6 files changed

+304
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# myframe

index.htm

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="pt">
4+
5+
<head>
6+
<title>myFrame</title>
7+
8+
</head>
9+
10+
11+
12+
<body>
13+
14+
15+
<script>
16+
let scripts = {}
17+
</script>
18+
19+
<script src="myFrame/myFrame.js"></script>
20+
<script src="myFrame/testMyFrame.js"></script>
21+
<script src="main.js"></script>
22+
23+
24+
</body>
25+
</html>
26+
27+
28+
29+

main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const testMyFrame = scripts['myFrame/testMyFrame.js']
2+
3+
4+
requestAnimationFrame(testMyFrame)

myFrame/myFrame.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
scripts['myFrame/myFrame.js'] = (function createMyFrame () {
2+
3+
let arrayOfFrameObjs = [createFrameObj(), createFrameObj(), createFrameObj()],
4+
requestedTest = false
5+
6+
7+
const module = {}
8+
9+
10+
module.requestResponse = (fun, stage) => {
11+
add(arrayOfFrameObjs[0], fun, stage)
12+
}
13+
14+
module.requestNext = (fun, stage) => {
15+
add(arrayOfFrameObjs[1], fun, stage)
16+
}
17+
18+
module.requestLast = (fun, stage) => {
19+
add(arrayOfFrameObjs[2], fun, stage)
20+
}
21+
22+
23+
return module
24+
25+
26+
27+
28+
function createFrameObj () {
29+
return {read: [], write: [], isEmpty: true}
30+
}
31+
32+
33+
function add (frameObj, fun, stage = 'write') {
34+
35+
if(stage == 'read') {
36+
frameObj.read.push(fun)
37+
}
38+
else
39+
if(stage == 'write') {
40+
frameObj.write.push(fun)
41+
}
42+
43+
if(frameObj.isEmpty) {
44+
frameObj.isEmpty = false
45+
}
46+
47+
if(!requestedTest){
48+
requestedTest = true
49+
requestAnimationFrame(test)
50+
}
51+
52+
}
53+
54+
55+
56+
function test () {
57+
let testIndex = 0,
58+
foundPopulatedFrame = false,
59+
populatedFrameIndex = undefined
60+
61+
62+
63+
do{
64+
var currentFrameObj = arrayOfFrameObjs[testIndex]
65+
66+
if(currentFrameObj.isEmpty) {
67+
testIndex++
68+
}
69+
else {
70+
foundPopulatedFrame = true
71+
populatedFrameIndex = testIndex
72+
break;
73+
}
74+
}
75+
while(testIndex <= 2)
76+
77+
78+
79+
if(foundPopulatedFrame){
80+
consumeFrameObj(populatedFrameIndex)
81+
requestAnimationFrame(test)
82+
}
83+
else{
84+
requestedTest = false
85+
}
86+
87+
}
88+
89+
90+
91+
function consumeFrameObj (frameObjIndex){
92+
93+
const currentFrameObj = arrayOfFrameObjs[frameObjIndex]
94+
arrayOfFrameObjs[frameObjIndex] = createFrameObj()
95+
96+
const arrayOfReadFuns = currentFrameObj.read
97+
const arrayOfWriteFuns = currentFrameObj.write
98+
99+
100+
while (arrayOfReadFuns.length) {
101+
arrayOfReadFuns.shift()()
102+
}
103+
104+
while (arrayOfWriteFuns.length) {
105+
arrayOfWriteFuns.shift()()
106+
}
107+
108+
}
109+
110+
111+
})()

myFrame/testMyFrame.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
scripts['myFrame/testMyFrame.js'] = function testMyFrame () {
2+
3+
const requestResponse = scripts['myFrame/myFrame.js'].requestResponse
4+
const requestNext = scripts['myFrame/myFrame.js'].requestNext
5+
const requestLast = scripts['myFrame/myFrame.js'].requestLast
6+
7+
let resultString = ''
8+
9+
10+
const arrayOfRequests = [
11+
()=>{
12+
requestResponse(() =>{
13+
testResult(1, 1)
14+
}, 'read')
15+
},
16+
()=>{
17+
requestResponse(()=>{
18+
testResult(1, 2)
19+
}, 'write')
20+
},
21+
22+
23+
()=>{
24+
requestNext(()=>{
25+
testResult(2, 1)
26+
}, 'read')
27+
},
28+
()=>{
29+
requestNext(()=>{
30+
testResult(2, 2)
31+
32+
requestNext(()=>{
33+
testResult(3, 2)
34+
})
35+
36+
requestNext(()=>{
37+
testResult(3, 3)
38+
})
39+
40+
requestLast(()=>{
41+
testResult(4, 3)
42+
showResult()
43+
})
44+
45+
requestNext(()=>{
46+
testResult(3, 1)
47+
}, 'read')
48+
49+
}, 'write')
50+
},
51+
52+
53+
()=>{
54+
requestLast(()=>{
55+
testResult(4, 1)
56+
}, 'read')
57+
},
58+
()=>{
59+
requestLast(()=>{
60+
testResult(4, 2)
61+
}, 'write')
62+
}
63+
]
64+
65+
66+
console.log("DISCLAIMER: In good contditions of tremperature and presssure the diference in time expected " +
67+
"for diferente animationFrames will match and as far as I know there isn't any other way to detect " +
68+
"if two functions were called in diferent frames so that will have to do.")
69+
70+
71+
72+
let lastResult = false
73+
74+
function testResult (frameNumber, funNumber) {
75+
const currResult = {frameNumber, funNumber, time: Date.now()}
76+
77+
78+
if(!lastResult) { //isEmpty
79+
lastResult = currResult
80+
resultString += JSON.stringify(currResult) + ' ok first\n'
81+
return
82+
}
83+
84+
85+
if(currResult.frameNumber < lastResult.frameNumber){
86+
console.log(currResult)
87+
throw "Frame in wrong order"
88+
89+
}
90+
else if(currResult.frameNumber > lastResult.frameNumber){
91+
92+
if(currResult.time - lastResult.time < 10){
93+
console.log(currResult)
94+
throw "This fun should be executed in another frame"
95+
96+
}
97+
else {
98+
resultString += JSON.stringify(currResult) + ' ok newFrame\n'
99+
}
100+
101+
}
102+
else {
103+
104+
if(currResult.funNumber < lastResult.funNumber){
105+
console.log(currResult)
106+
throw "Function in wrong order"
107+
108+
109+
}
110+
else if(currResult.funNumber > lastResult.funNumber){
111+
112+
if(currResult.time - lastResult.time > 10){
113+
console.log(currResult)
114+
throw "This fun either took too long or was executed in another frame but shouldn't be"
115+
116+
}
117+
else {
118+
resultString += JSON.stringify(currResult) + ' ok newFun\n'
119+
}
120+
121+
}
122+
else {
123+
console.log(currResult)
124+
throw "For some reason this function was executed twice"
125+
126+
}
127+
128+
}
129+
130+
lastResult = currResult
131+
132+
}
133+
134+
135+
shuffle(arrayOfRequests)
136+
137+
arrayOfRequests.forEach(x=>x())
138+
139+
function showResult () {
140+
document.body.innerText = resultString
141+
}
142+
143+
144+
function shuffle(a) {
145+
var j, x, i;
146+
for (i = a.length - 1; i > 0; i--) {
147+
j = Math.floor(Math.random() * (i + 1));
148+
x = a[i];
149+
a[i] = a[j];
150+
a[j] = x;
151+
}
152+
}
153+
154+
155+
156+
157+
}

0 commit comments

Comments
 (0)