Best JavaScript code snippet using wpt
unread.js
Source:unread.js
...29 subscribed: true,30 is_muted: false,31};32stream_data.add_sub(social);33function assert_zero_counts(counts) {34 assert.equal(counts.private_message_count, 0);35 assert.equal(counts.home_unread_messages, 0);36 assert.equal(counts.mentioned_message_count, 0);37 assert.equal(counts.stream_count.size, 0);38 assert.equal(counts.pm_count.size, 0);39}40function test_notifiable_count(home_unread_messages, expected_notifiable_count) {41 page_params = set_global("page_params", {42 desktop_icon_count_display: 1,43 });44 let notifiable_counts = unread.get_notifiable_count();45 assert.deepEqual(notifiable_counts, home_unread_messages);46 page_params = set_global("page_params", {47 desktop_icon_count_display: 2,48 });49 notifiable_counts = unread.get_notifiable_count();50 assert.deepEqual(notifiable_counts, expected_notifiable_count);51 page_params = set_global("page_params", {52 desktop_icon_count_display: 3,53 });54 notifiable_counts = unread.get_notifiable_count();55 assert.deepEqual(notifiable_counts, 0);56}57function test(label, f) {58 run_test(label, (override) => {59 unread.declare_bankruptcy();60 muting.set_muted_topics([]);61 f(override);62 });63}64test("empty_counts_while_narrowed", () => {65 const counts = unread.get_counts();66 assert_zero_counts(counts);67 test_notifiable_count(counts.home_unread_messages, 0);68});69test("empty_counts_while_home", () => {70 const counts = unread.get_counts();71 assert_zero_counts(counts);72 test_notifiable_count(counts.home_unread_messages, 0);73});74test("changing_topics", (override) => {75 // Summary: change the topic of a message from 'lunch'76 // to 'dinner' using update_unread_topics().77 let count = unread.num_unread_for_topic(social.stream_id, "lunch");78 assert.equal(count, 0);79 const stream_id = 100;80 const wrong_stream_id = 110;81 const message = {82 id: 15,83 type: "stream",84 stream_id,85 topic: "luNch",86 unread: true,87 };88 const other_message = {89 id: 16,90 type: "stream",91 stream_id,92 topic: "lunCH",93 unread: true,94 };95 assert.deepEqual(unread.get_unread_message_ids([15, 16]), []);96 assert.deepEqual(unread.get_unread_messages([message, other_message]), []);97 let msg_ids = unread.get_msg_ids_for_topic(stream_id, "LuNcH");98 assert.deepEqual(msg_ids, []);99 msg_ids = unread.get_msg_ids_for_stream(stream_id);100 assert.deepEqual(msg_ids, []);101 unread.process_loaded_messages([message, other_message]);102 assert.deepEqual(unread.get_all_msg_ids(), [15, 16]);103 assert.deepEqual(unread.get_unread_message_ids([15, 16]), [15, 16]);104 assert.deepEqual(unread.get_unread_messages([message, other_message]), [105 message,106 other_message,107 ]);108 count = unread.num_unread_for_topic(stream_id, "Lunch");109 assert.equal(count, 2);110 assert(unread.topic_has_any_unread(stream_id, "lunch"));111 assert(!unread.topic_has_any_unread(wrong_stream_id, "lunch"));112 assert(!unread.topic_has_any_unread(stream_id, "NOT lunch"));113 count = unread.num_unread_for_topic(stream_id, "NOT lunch");114 assert.equal(count, 0);115 msg_ids = unread.get_msg_ids_for_topic(stream_id, "NOT lunch");116 assert.deepEqual(msg_ids, []);117 let event = {118 topic: "dinner",119 };120 unread.update_unread_topics(message, event);121 count = unread.num_unread_for_topic(stream_id, "lUnch");122 assert.equal(count, 1);123 count = unread.num_unread_for_topic(stream_id, "dinner");124 assert.equal(count, 1);125 event = {126 topic: "snack",127 };128 unread.update_unread_topics(other_message, event);129 count = unread.num_unread_for_topic(stream_id, "lunch");130 assert.equal(count, 0);131 assert(!unread.topic_has_any_unread(stream_id, "lunch"));132 assert(!unread.topic_has_any_unread(wrong_stream_id, "lunch"));133 count = unread.num_unread_for_topic(stream_id, "snack");134 assert.equal(count, 1);135 assert(unread.topic_has_any_unread(stream_id, "snack"));136 assert(!unread.topic_has_any_unread(wrong_stream_id, "snack"));137 // Test defensive code. Trying to update a message we don't know138 // about should be a no-op.139 event = {140 topic: "brunch",141 };142 unread.update_unread_topics(other_message, event);143 // Update a message that was never marked as unread.144 const sticky_message = {145 id: 17,146 type: "stream",147 stream_id,148 topic: "sticky",149 unread: true,150 };151 const message_dict = new Map();152 message_dict.set(message.id, message);153 message_dict.set(other_message.id, other_message);154 message_dict.set(sticky_message.id, sticky_message);155 override(message_store, "get", (msg_id) => message_dict.get(msg_id));156 unread.process_loaded_messages([sticky_message]);157 count = unread.num_unread_for_topic(stream_id, "sticky");158 assert.equal(count, 1);159 assert(sticky_message.unread);160 unread.mark_as_read(sticky_message.id);161 count = unread.num_unread_for_topic(stream_id, "sticky");162 assert.equal(count, 0);163 assert(!sticky_message.unread);164 event = {165 topic: "sticky",166 };167 unread.update_unread_topics(sticky_message, event);168 count = unread.num_unread_for_topic(stream_id, "sticky");169 assert.equal(count, 0);170 // cleanup171 unread.mark_as_read(message.id);172 count = unread.num_unread_for_topic(stream_id, "dinner");173 assert.equal(count, 0);174 unread.mark_as_read(other_message.id);175 count = unread.num_unread_for_topic(stream_id, "snack");176 assert.equal(count, 0);177 // test coverage178 unread.update_unread_topics(sticky_message, {});179});180test("muting", () => {181 const stream_id = social.stream_id;182 const unknown_stream_id = 555;183 const message = {184 id: 15,185 type: "stream",186 stream_id,187 topic: "test_muting",188 unread: true,189 };190 unread.process_loaded_messages([message]);191 let counts = unread.get_counts();192 assert.equal(counts.stream_count.get(stream_id), 1);193 assert.equal(counts.home_unread_messages, 1);194 assert.equal(unread.num_unread_for_stream(stream_id), 1);195 assert.deepEqual(unread.get_msg_ids_for_stream(stream_id), [message.id]);196 test_notifiable_count(counts.home_unread_messages, 0);197 muting.add_muted_topic(social.stream_id, "test_muting");198 counts = unread.get_counts();199 assert.equal(counts.stream_count.get(stream_id), 0);200 assert.equal(counts.home_unread_messages, 0);201 assert.equal(unread.num_unread_for_stream(stream_id), 0);202 assert.deepEqual(unread.get_msg_ids_for_stream(stream_id), []);203 test_notifiable_count(counts.home_unread_messages, 0);204 // we still find the message id here (muting is ignored)205 assert.deepEqual(unread.get_all_msg_ids(), [message.id]);206 assert.equal(unread.num_unread_for_stream(unknown_stream_id), 0);207});208test("num_unread_for_topic", (override) => {209 // Test the num_unread_for_topic() function using many210 // messages.211 const stream_id = 301;212 override(stream_data, "get_sub_by_id", (arg) => {213 if (arg === stream_id) {214 return {name: "Some Stream"};215 }216 throw new Error(`Unknown stream ${arg}`);217 });218 let count = unread.num_unread_for_topic(stream_id, "lunch");219 assert.equal(count, 0);220 const message = {221 type: "stream",222 stream_id,223 topic: "LuncH",224 unread: true,225 };226 // Put messages into list in reverse order to try to confuse227 // our sort.228 const num_msgs = 500;229 let i;230 for (i = num_msgs; i > 0; i -= 1) {231 message.id = i;232 unread.process_loaded_messages([message]);233 }234 count = unread.num_unread_for_topic(stream_id, "lunch");235 assert.equal(count, num_msgs);236 let msg_ids = unread.get_msg_ids_for_topic(stream_id, "LuNcH");237 assert.deepEqual(msg_ids, _.range(1, 501));238 msg_ids = unread.get_msg_ids_for_stream(stream_id);239 assert.deepEqual(msg_ids, _.range(1, 501));240 const topic_dict = new FoldDict();241 let missing_topics = unread.get_missing_topics({242 stream_id,243 topic_dict,244 });245 assert.deepEqual(missing_topics, [{pretty_name: "LuncH", message_id: 500}]);246 topic_dict.set("lUNCh", "whatever");247 missing_topics = unread.get_missing_topics({248 stream_id,249 topic_dict,250 });251 assert.deepEqual(missing_topics, []);252 for (i = 0; i < num_msgs; i += 1) {253 message.id = i + 1;254 unread.mark_as_read(message.id);255 }256 count = unread.num_unread_for_topic(stream_id, "lunch");257 assert.equal(count, 0);258 msg_ids = unread.get_msg_ids_for_topic(stream_id, "LuNcH");259 assert.deepEqual(msg_ids, []);260 msg_ids = unread.get_msg_ids_for_stream(stream_id);261 assert.deepEqual(msg_ids, []);262});263test("home_messages", (override) => {264 override(stream_data, "is_subscribed", () => true);265 override(stream_data, "is_muted", () => false);266 const stream_id = 401;267 override(stream_data, "get_sub_by_id", () => ({268 name: "whatever",269 }));270 const message = {271 id: 15,272 type: "stream",273 stream_id,274 topic: "lunch",275 unread: true,276 };277 let counts = unread.get_counts();278 assert.equal(counts.home_unread_messages, 0);279 test_notifiable_count(counts.home_unread_messages, 0);280 unread.process_loaded_messages([message]);281 counts = unread.get_counts();282 assert.equal(counts.home_unread_messages, 1);283 assert.equal(counts.stream_count.get(stream_id), 1);284 test_notifiable_count(counts.home_unread_messages, 0);285 unread.mark_as_read(message.id);286 counts = unread.get_counts();287 assert.equal(counts.home_unread_messages, 0);288 test_notifiable_count(counts.home_unread_messages, 0);289 unread.process_loaded_messages([message]);290 counts = unread.get_counts();291 assert.equal(counts.home_unread_messages, 1);292 test_notifiable_count(counts.home_unread_messages, 0);293 // Now unsubscribe all our streams.294 override(stream_data, "is_subscribed", () => false);295 counts = unread.get_counts();296 assert.equal(counts.home_unread_messages, 0);297 test_notifiable_count(counts.home_unread_messages, 0);298});299test("phantom_messages", () => {300 const message = {301 id: 999,302 type: "stream",303 stream_id: 555,304 topic: "phantom",305 };306 unread.mark_as_read(message.id);307 const counts = unread.get_counts();308 assert.equal(counts.home_unread_messages, 0);309 test_notifiable_count(counts.home_unread_messages, 0);310});311test("private_messages", () => {312 let counts = unread.get_counts();313 assert.equal(counts.private_message_count, 0);314 const anybody = {315 email: "anybody@example.com",316 user_id: 999,317 full_name: "Any Body",318 };319 people.add_active_user(anybody);320 const message = {321 id: 15,322 type: "private",323 display_recipient: [{id: anybody.user_id}, {id: me.user_id}],324 unread: true,325 };326 unread.process_loaded_messages([message]);327 counts = unread.get_counts();328 assert.equal(counts.private_message_count, 1);329 assert.equal(counts.pm_count.get("999"), 1);330 test_notifiable_count(counts.home_unread_messages, 1);331 unread.mark_as_read(message.id);332 counts = unread.get_counts();333 assert.equal(counts.private_message_count, 0);334 assert.equal(counts.pm_count.get("999"), 0);335 test_notifiable_count(counts.home_unread_messages, 0);336});337test("private_messages", () => {338 const alice = {339 email: "alice@example.com",340 user_id: 101,341 full_name: "Alice",342 };343 people.add_active_user(alice);344 const bob = {345 email: "bob@example.com",346 user_id: 102,347 full_name: "Bob",348 };349 people.add_active_user(bob);350 assert.equal(unread.num_unread_for_person(alice.user_id.toString()), 0);351 assert.equal(unread.num_unread_for_person(bob.user_id.toString()), 0);352 assert.deepEqual(unread.get_msg_ids_for_person(alice.user_id.toString()), []);353 assert.deepEqual(unread.get_msg_ids_for_person(bob.user_id.toString()), []);354 assert.deepEqual(unread.get_msg_ids_for_person(), []);355 assert.deepEqual(unread.get_msg_ids_for_private(), []);356 const message = {357 id: 15,358 display_recipient: [{id: alice.user_id}],359 type: "private",360 unread: true,361 };362 const read_message = {363 flags: ["read"],364 };365 unread.process_loaded_messages([message, read_message]);366 assert.equal(unread.num_unread_for_person(alice.user_id.toString()), 1);367 assert.equal(unread.num_unread_for_person(""), 0);368 assert.deepEqual(unread.get_msg_ids_for_person(alice.user_id.toString()), [message.id]);369 assert.deepEqual(unread.get_msg_ids_for_person(bob.user_id.toString()), []);370 assert.deepEqual(unread.get_msg_ids_for_private(), [message.id]);371 assert.deepEqual(unread.get_all_msg_ids(), [message.id]);372 unread.mark_as_read(message.id);373 assert.equal(unread.num_unread_for_person(alice.user_id.toString()), 0);374 assert.equal(unread.num_unread_for_person(""), 0);375 assert.deepEqual(unread.get_msg_ids_for_person(alice.user_id.toString()), []);376 assert.deepEqual(unread.get_msg_ids_for_person(bob.user_id.toString()), []);377 assert.deepEqual(unread.get_msg_ids_for_private(), []);378 assert.deepEqual(unread.get_all_msg_ids(), []);379 const counts = unread.get_counts();380 assert.equal(counts.private_message_count, 0);381 test_notifiable_count(counts.home_unread_messages, 0);382});383test("mentions", () => {384 let counts = unread.get_counts();385 assert.equal(counts.mentioned_message_count, 0);386 assert.deepEqual(unread.get_msg_ids_for_mentions(), []);387 test_notifiable_count(counts.home_unread_messages, 0);388 const muted_stream_id = 401;389 muting.add_muted_topic(401, "lunch");390 const already_read_message = {391 id: 14,392 type: "stream",393 stream_id: 400,394 topic: "lunch",395 mentioned: true,396 mentioned_me_directly: true,397 unread: false,398 };399 const mention_me_message = {400 id: 15,401 type: "stream",402 stream_id: 400,403 topic: "lunch",404 mentioned: true,405 mentioned_me_directly: true,406 unread: true,407 };408 const mention_all_message = {409 id: 16,410 type: "stream",411 stream_id: 400,412 topic: "lunch",413 mentioned: true,414 mentioned_me_directly: false,415 unread: true,416 };417 // This message shouldn't affect the unread mention counts.418 const muted_mention_all_message = {419 id: 17,420 type: "stream",421 stream_id: muted_stream_id,422 topic: "lunch",423 mentioned: true,424 mentioned_me_directly: false,425 unread: true,426 };427 unread.process_loaded_messages([428 already_read_message,429 mention_me_message,430 mention_all_message,431 muted_mention_all_message,432 ]);433 counts = unread.get_counts();434 assert.equal(counts.mentioned_message_count, 2);435 assert.deepEqual(unread.get_msg_ids_for_mentions(), [436 mention_me_message.id,437 mention_all_message.id,438 ]);439 assert.deepEqual(unread.get_all_msg_ids(), [440 mention_me_message.id,441 mention_all_message.id,442 muted_mention_all_message.id,443 ]);444 test_notifiable_count(counts.home_unread_messages, 2);445 unread.mark_as_read(mention_me_message.id);446 unread.mark_as_read(mention_all_message.id);447 counts = unread.get_counts();448 assert.equal(counts.mentioned_message_count, 0);449 test_notifiable_count(counts.home_unread_messages, 0);450});451test("mention updates", () => {452 const message = {453 id: 17,454 unread: false,455 type: "stream",456 };457 function test_counted(counted) {458 unread.update_message_for_mention(message);459 assert.equal(unread.unread_mentions_counter.has(message.id), counted);460 }461 test_counted(false);462 message.unread = true;463 message.mentioned = true;464 test_counted(true);465 message.mentioned = false;466 test_counted(false);467 message.mentioned = true;468 test_counted(true);469 message.unread = false;470 test_counted(false);471 message.unread = true;472 test_counted(true);473});474test("starring", () => {475 // We don't need any setup here, because we just hard code476 // this to [] in the code.477 assert.deepEqual(unread.get_msg_ids_for_starred(), []);478});479test("declare_bankruptcy", () => {480 const message = {481 id: 16,482 type: "whatever",483 stream_id: 1999,484 topic: "whatever",485 mentioned: true,486 };487 unread.process_loaded_messages([message]);488 unread.declare_bankruptcy();489 const counts = unread.get_counts();490 assert_zero_counts(counts);491 test_notifiable_count(counts.home_unread_messages, 0);492});493test("message_unread", () => {494 // Test some code that might be overly defensive, for line coverage sake.495 assert(!unread.message_unread(undefined));496 assert(unread.message_unread({unread: true}));497 assert(!unread.message_unread({unread: false}));498});499test("server_counts", () => {500 // note that user_id 30 is "me"501 page_params.unread_msgs = {502 pms: [503 {504 sender_id: 101,505 unread_message_ids: [31, 32, 60, 61, 62, 63],506 },507 ],508 huddles: [509 {510 user_ids_string: "4,6,30,101",511 unread_message_ids: [34, 50],512 },513 ],514 streams: [515 {516 stream_id: 1,517 topic: "test",518 unread_message_ids: [33, 35, 36],519 },520 ],521 mentions: [31, 34, 40, 41],522 };523 unread.initialize();524 assert.equal(unread.num_unread_for_person("101"), 6);525 assert.equal(unread.num_unread_for_person("4,6,101"), 2);526 assert.equal(unread.num_unread_for_person("30"), 0);527 assert.equal(unread.num_unread_for_topic(0, "bogus"), 0);528 assert.equal(unread.num_unread_for_topic(1, "bogus"), 0);529 assert.equal(unread.num_unread_for_topic(1, "test"), 3);530 assert.equal(unread.unread_mentions_counter.size, 4);531 unread.mark_as_read(40);532 assert.equal(unread.unread_mentions_counter.size, 3);533 unread.mark_as_read(35);534 assert.equal(unread.num_unread_for_topic(1, "test"), 2);535 unread.mark_as_read(34);536 assert.equal(unread.num_unread_for_person("4,6,101"), 1);537});538test("empty_cases", () => {539 const stream_id = 999;540 let msg_ids = unread.get_msg_ids_for_topic(stream_id, "LuNcH");541 assert.deepEqual(msg_ids, []);542 msg_ids = unread.get_msg_ids_for_stream(stream_id);543 assert.deepEqual(msg_ids, []);544 assert.deepEqual(unread.get_all_msg_ids(), []);545 const missing_topics = unread.get_missing_topics({546 stream_id,547 topic_dict: "should-never-be-referenced",548 });549 assert.deepEqual(missing_topics, []);550});551test("errors", () => {552 // Test unknown message leads to zero count553 const message = {554 id: 9,555 type: "private",556 display_recipient: [{id: 9999}],557 };558 unread.mark_as_read(message.id);559 const counts = unread.get_counts();560 assert.equal(counts.private_message_count, 0);561 test_notifiable_count(counts.home_unread_messages, 0);...
autoplay.test.js
Source:autoplay.test.js
1/* eslint-env qunit */2import Player from '../../src/js/player.js';3import videojs from '../../src/js/video.js';4import TestHelpers from './test-helpers.js';5import document from 'global/document';6import window from 'global/window';7import sinon from 'sinon';8QUnit.module('autoplay', {9 beforeEach() {10 this.clock = sinon.useFakeTimers();11 // reset players storage12 for (const playerId in Player.players) {13 if (Player.players[playerId] !== null) {14 Player.players[playerId].dispose();15 }16 delete Player.players[playerId];17 }18 const videoTag = TestHelpers.makeTag();19 const fixture = document.getElementById('qunit-fixture');20 this.counts = {21 play: 0,22 muted: 0,23 success: 0,24 failure: 025 };26 fixture.appendChild(videoTag);27 // These mock promises immediately execute,28 // effectively synchronising promise chains for testing29 // This will only act on catch calls30 this.rejectPromise = {31 then(fn) {32 return this;33 },34 catch(fn) {35 try {36 fn();37 } catch (err) {38 return this;39 }40 return this;41 }42 };43 // This will only act on then calls44 this.resolvePromise = {45 then(fn) {46 fn();47 return this;48 },49 catch(fn) {50 return this;51 }52 };53 this.createPlayer = (options = {}, attributes = {}, playRetval = null) => {54 Object.keys(attributes).forEach((a) => {55 videoTag.setAttribute(a, attributes[a]);56 });57 this.player = videojs(videoTag.id, videojs.mergeOptions({techOrder: ['techFaker']}, options));58 const oldMuted = this.player.muted;59 this.player.play = () => {60 this.counts.play++;61 if (playRetval || this.playRetval) {62 return playRetval || this.playRetval;63 }64 };65 this.mutedValue = this.player.muted();66 this.player.muted = (v) => {67 if (typeof v !== 'undefined') {68 this.counts.muted++;69 this.mutedValue = v;70 }71 return oldMuted.call(this.player, v);72 };73 this.player.on('autoplay-success', () => this.counts.success++);74 this.player.on('autoplay-failure', () => this.counts.failure++);75 // we have to trigger ready so that we76 // are waiting for loadstart77 this.player.tech_.triggerReady();78 return this.player;79 };80 },81 afterEach() {82 this.clock.restore();83 this.player.dispose();84 }85});86QUnit.test('option = false no play/muted', function(assert) {87 this.createPlayer({autoplay: false});88 assert.equal(this.player.autoplay(), false, 'player.autoplay getter');89 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');90 this.player.tech_.trigger('loadstart');91 assert.equal(this.counts.play, 0, 'play count');92 assert.equal(this.counts.muted, 0, 'muted count');93 assert.equal(this.counts.success, 0, 'success count');94 assert.equal(this.counts.failure, 0, 'failure count');95 this.player.tech_.trigger('loadstart');96 assert.equal(this.counts.play, 0, 'play count');97 assert.equal(this.counts.muted, 0, 'muted count');98 assert.equal(this.counts.success, 0, 'success count');99 assert.equal(this.counts.failure, 0, 'failure count');100});101QUnit.test('option = true no play/muted', function(assert) {102 this.createPlayer({autoplay: true});103 assert.equal(this.player.autoplay(), true, 'player.autoplay getter');104 assert.equal(this.player.tech_.autoplay(), true, 'tech.autoplay getter');105 this.player.tech_.trigger('loadstart');106 assert.equal(this.counts.play, 0, 'play count');107 assert.equal(this.counts.muted, 0, 'muted count');108 assert.equal(this.counts.success, 0, 'success count');109 assert.equal(this.counts.failure, 0, 'failure count');110 this.player.tech_.trigger('loadstart');111 assert.equal(this.counts.play, 0, 'play count');112 assert.equal(this.counts.muted, 0, 'muted count');113 assert.equal(this.counts.success, 0, 'success count');114 assert.equal(this.counts.failure, 0, 'failure count');115});116QUnit.test('option = "random" no play/muted', function(assert) {117 this.createPlayer({autoplay: 'random'});118 assert.equal(this.player.autoplay(), true, 'player.autoplay getter');119 assert.equal(this.player.tech_.autoplay(), true, 'tech.autoplay getter');120 this.player.tech_.trigger('loadstart');121 assert.equal(this.counts.play, 0, 'play count');122 assert.equal(this.counts.muted, 0, 'muted count');123 assert.equal(this.counts.success, 0, 'success count');124 assert.equal(this.counts.failure, 0, 'failure count');125 this.player.tech_.trigger('loadstart');126 assert.equal(this.counts.play, 0, 'play count');127 assert.equal(this.counts.muted, 0, 'muted count');128 assert.equal(this.counts.success, 0, 'success count');129 assert.equal(this.counts.failure, 0, 'failure count');130});131QUnit.test('option = null, should be set to false no play/muted', function(assert) {132 this.createPlayer({autoplay: null});133 assert.equal(this.player.autoplay(), false, 'player.autoplay getter');134 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');135 this.player.tech_.trigger('loadstart');136 assert.equal(this.counts.play, 0, 'play count');137 assert.equal(this.counts.muted, 0, 'muted count');138 assert.equal(this.counts.success, 0, 'success count');139 assert.equal(this.counts.failure, 0, 'failure count');140 this.player.tech_.trigger('loadstart');141 assert.equal(this.counts.play, 0, 'play count');142 assert.equal(this.counts.muted, 0, 'muted count');143 assert.equal(this.counts.success, 0, 'success count');144 assert.equal(this.counts.failure, 0, 'failure count');145});146QUnit.test('option = "play" play, no muted', function(assert) {147 this.createPlayer({autoplay: 'play'}, {}, this.resolvePromise);148 assert.equal(this.player.autoplay(), 'play', 'player.autoplay getter');149 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');150 this.player.tech_.trigger('loadstart');151 assert.equal(this.counts.play, 1, 'play count');152 assert.equal(this.counts.muted, 0, 'muted count');153 assert.equal(this.counts.success, 1, 'success count');154 assert.equal(this.counts.failure, 0, 'failure count');155 this.player.tech_.trigger('loadstart');156 assert.equal(this.counts.play, 2, 'play count');157 assert.equal(this.counts.muted, 0, 'muted count');158 assert.equal(this.counts.success, 2, 'success count');159 assert.equal(this.counts.failure, 0, 'failure count');160});161QUnit.test('option = true w/ normalizeAutoplay = true play, no muted', function(assert) {162 this.createPlayer({163 autoplay: true,164 normalizeAutoplay: true165 }, {}, this.resolvePromise);166 assert.equal(this.player.autoplay(), true, 'player.autoplay getter');167 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');168 this.player.tech_.trigger('loadstart');169 assert.equal(this.counts.play, 1, 'play count');170 assert.equal(this.counts.muted, 0, 'muted count');171 assert.equal(this.counts.success, 1, 'success count');172 assert.equal(this.counts.failure, 0, 'failure count');173 this.player.tech_.trigger('loadstart');174 assert.equal(this.counts.play, 2, 'play count');175 assert.equal(this.counts.muted, 0, 'muted count');176 assert.equal(this.counts.success, 2, 'success count');177 assert.equal(this.counts.failure, 0, 'failure count');178});179QUnit.test('option = "any" play, no muted', function(assert) {180 this.createPlayer({autoplay: 'any'}, {}, this.resolvePromise);181 assert.equal(this.player.autoplay(), 'any', 'player.autoplay getter');182 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');183 this.player.tech_.trigger('loadstart');184 assert.equal(this.counts.play, 1, 'play count');185 assert.equal(this.counts.muted, 0, 'muted count');186 assert.equal(this.counts.success, 1, 'success count');187 assert.equal(this.counts.failure, 0, 'failure count');188 this.player.tech_.trigger('loadstart');189 assert.equal(this.counts.play, 2, 'play count');190 assert.equal(this.counts.muted, 0, 'muted count');191 assert.equal(this.counts.success, 2, 'success count');192 assert.equal(this.counts.failure, 0, 'failure count');193});194QUnit.test('option = "muted" play and muted', function(assert) {195 this.createPlayer({autoplay: 'muted'}, {}, this.resolvePromise);196 assert.equal(this.player.autoplay(), 'muted', 'player.autoplay getter');197 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');198 this.player.tech_.trigger('loadstart');199 assert.equal(this.counts.play, 1, 'play count');200 assert.equal(this.counts.muted, 1, 'muted count');201 assert.equal(this.counts.success, 1, 'success count');202 assert.equal(this.counts.failure, 0, 'failure count');203 this.player.tech_.trigger('loadstart');204 assert.equal(this.counts.play, 2, 'play count');205 assert.equal(this.counts.muted, 2, 'muted count');206 assert.equal(this.counts.success, 2, 'success count');207 assert.equal(this.counts.failure, 0, 'failure count');208});209QUnit.test('option = "play" play, no muted, rejection ignored', function(assert) {210 this.createPlayer({autoplay: 'play'}, {}, this.rejectPromise);211 assert.equal(this.player.autoplay(), 'play', 'player.autoplay getter');212 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');213 this.player.tech_.trigger('loadstart');214 assert.equal(this.counts.play, 1, 'play count');215 assert.equal(this.counts.muted, 0, 'muted count');216 assert.equal(this.counts.success, 0, 'success count');217 assert.equal(this.counts.failure, 1, 'failure count');218 this.player.tech_.trigger('loadstart');219 assert.equal(this.counts.play, 2, 'play count');220 assert.equal(this.counts.muted, 0, 'muted count');221 assert.equal(this.counts.success, 0, 'success count');222 assert.equal(this.counts.failure, 2, 'failure count');223});224QUnit.test('option = "any" play, no muted, rejection leads to muted then play', function(assert) {225 this.createPlayer({autoplay: 'any'}, {}, this.rejectPromise);226 assert.equal(this.player.autoplay(), 'any', 'player.autoplay getter');227 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');228 // The workflow described here:229 // Call play() -> on rejection, attempt to set mute to true ->230 // call play() again -> on rejection, set original mute value ->231 // catch failure at the end of promise chain232 this.player.tech_.trigger('loadstart');233 assert.equal(this.counts.play, 2, 'play count');234 assert.equal(this.counts.muted, 2, 'muted count');235 assert.equal(this.counts.success, 0, 'success count');236 assert.equal(this.counts.failure, 1, 'failure count');237 this.player.tech_.trigger('loadstart');238 assert.equal(this.counts.play, 4, 'play count');239 assert.equal(this.counts.muted, 4, 'muted count');240 assert.equal(this.counts.success, 0, 'success count');241 assert.equal(this.counts.failure, 2, 'failure count');242});243QUnit.test('option = "muted" play and muted, rejection ignored', function(assert) {244 this.createPlayer({autoplay: 'muted'}, {}, this.rejectPromise);245 assert.equal(this.player.autoplay(), 'muted', 'player.autoplay getter');246 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');247 // muted called twice here, as muted is value is restored on failure.248 this.player.tech_.trigger('loadstart');249 assert.equal(this.counts.play, 1, 'play count');250 assert.equal(this.counts.muted, 2, 'muted count');251 assert.equal(this.counts.success, 0, 'success count');252 assert.equal(this.counts.failure, 1, 'failure count');253 this.player.tech_.trigger('loadstart');254 assert.equal(this.counts.play, 2, 'play count');255 assert.equal(this.counts.muted, 4, 'muted count');256 assert.equal(this.counts.success, 0, 'success count');257 assert.equal(this.counts.failure, 2, 'failure count');258});259QUnit.test('option = "muted", attr = true, play and muted', function(assert) {260 this.createPlayer({autoplay: 'muted'}, {autoplay: true});261 assert.equal(this.player.autoplay(), true, 'player.autoplay getter');262 assert.equal(this.player.tech_.autoplay(), true, 'tech.autoplay getter');263 this.player.tech_.trigger('loadstart');264 assert.equal(this.counts.play, 0, 'play count');265 assert.equal(this.counts.muted, 0, 'muted count');266 assert.equal(this.counts.success, 0, 'success count');267 assert.equal(this.counts.failure, 0, 'failure count');268 this.player.tech_.trigger('loadstart');269 assert.equal(this.counts.play, 0, 'play count');270 assert.equal(this.counts.muted, 0, 'muted count');271 assert.equal(this.counts.success, 0, 'success count');272 assert.equal(this.counts.failure, 0, 'failure count');273});274QUnit.test('option = "play", attr = true, play only', function(assert) {275 this.createPlayer({autoplay: 'play'}, {autoplay: true});276 assert.equal(this.player.autoplay(), true, 'player.autoplay getter');277 assert.equal(this.player.tech_.autoplay(), true, 'tech.autoplay getter');278 this.player.tech_.trigger('loadstart');279 assert.equal(this.counts.play, 0, 'play count');280 assert.equal(this.counts.muted, 0, 'muted count');281 assert.equal(this.counts.success, 0, 'success count');282 assert.equal(this.counts.failure, 0, 'failure count');283 this.player.tech_.trigger('loadstart');284 assert.equal(this.counts.play, 0, 'play count');285 assert.equal(this.counts.muted, 0, 'muted count');286 assert.equal(this.counts.success, 0, 'success count');287 assert.equal(this.counts.failure, 0, 'failure count');288});289QUnit.test('option = "any", attr = true, play only', function(assert) {290 this.createPlayer({autoplay: 'any'}, {autoplay: true});291 assert.equal(this.player.autoplay(), true, 'player.autoplay getter');292 assert.equal(this.player.tech_.autoplay(), true, 'tech.autoplay getter');293 this.player.tech_.trigger('loadstart');294 assert.equal(this.counts.play, 0, 'play count');295 assert.equal(this.counts.muted, 0, 'muted count');296 assert.equal(this.counts.success, 0, 'success count');297 assert.equal(this.counts.failure, 0, 'failure count');298 this.player.tech_.trigger('loadstart');299 assert.equal(this.counts.play, 0, 'play count');300 assert.equal(this.counts.muted, 0, 'muted count');301 assert.equal(this.counts.success, 0, 'success count');302 assert.equal(this.counts.failure, 0, 'failure count');303});304QUnit.test('option = "any", play terminated restores muted', function(assert) {305 this.createPlayer({autoplay: 'any'});306 this.playRetval = {307 then(fn) {308 fn();309 return this;310 },311 catch: (fn) => {312 assert.equal(this.counts.play, 1, 'play count');313 assert.equal(this.counts.muted, 0, 'muted count');314 assert.equal(this.counts.success, 0, 'success count');315 assert.equal(this.counts.failure, 0, 'failure count');316 this.playRetval = {317 then(_fn) {318 window.setTimeout(_fn, 1);319 return this;320 },321 catch(_fn) {322 return this;323 }324 };325 const retval = fn();326 assert.equal(this.counts.play, 2, 'play count');327 assert.equal(this.counts.muted, 1, 'muted count');328 assert.equal(this.mutedValue, true, 'is muted');329 assert.equal(this.counts.success, 0, 'success count');330 assert.equal(this.counts.failure, 0, 'failure count');331 return retval;332 }333 };334 assert.equal(this.player.autoplay(), 'any', 'player.autoplay getter');335 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');336 assert.equal(this.mutedValue, false, 'is not muted');337 this.player.tech_.trigger('loadstart');338 this.player.runPlayTerminatedQueue_();339 assert.equal(this.counts.play, 2, 'play count');340 assert.equal(this.counts.muted, 2, 'muted count');341 assert.equal(this.mutedValue, false, 'is not muted');342 assert.equal(this.counts.success, 0, 'success count');343 assert.equal(this.counts.failure, 0, 'failure count');344 this.player.runPlayTerminatedQueue_();345 assert.equal(this.counts.play, 2, 'play count');346 assert.equal(this.counts.muted, 2, 'muted count');347 assert.equal(this.mutedValue, false, 'is not muted');348 assert.equal(this.counts.success, 0, 'success count');349 assert.equal(this.counts.failure, 0, 'failure count');350 // verify autoplay success351 this.clock.tick(1);352 assert.equal(this.counts.play, 2, 'play count');353 assert.equal(this.counts.muted, 2, 'muted count');354 assert.equal(this.counts.success, 1, 'success count');355 assert.equal(this.counts.failure, 0, 'failure count');356});357QUnit.test('option = "muted", play terminated restores muted', function(assert) {358 this.createPlayer({autoplay: 'muted'}, {}, {359 then(fn) {360 window.setTimeout(() => {361 fn();362 }, 1);363 return this;364 },365 catch(fn) {366 return this;367 }368 });369 assert.equal(this.player.autoplay(), 'muted', 'player.autoplay getter');370 assert.equal(this.player.tech_.autoplay(), false, 'tech.autoplay getter');371 this.player.tech_.trigger('loadstart');372 assert.equal(this.counts.play, 1, 'play count');373 assert.equal(this.counts.muted, 1, 'muted count');374 assert.equal(this.mutedValue, true, 'is muted');375 assert.equal(this.counts.success, 0, 'success count');376 assert.equal(this.counts.failure, 0, 'failure count');377 this.player.runPlayTerminatedQueue_();378 assert.equal(this.counts.play, 1, 'play count');379 assert.equal(this.counts.muted, 2, 'muted count');380 assert.equal(this.mutedValue, false, 'no longer muted');381 assert.equal(this.counts.success, 0, 'success count');382 assert.equal(this.counts.failure, 0, 'failure count');383 // verify autoplay success384 this.clock.tick(1);385 assert.equal(this.counts.play, 1, 'play count');386 assert.equal(this.counts.muted, 2, 'muted count');387 assert.equal(this.counts.success, 1, 'success count');388 assert.equal(this.counts.failure, 0, 'failure count');...
script.js
Source:script.js
1(function (global) {2 "use strict";34 /*5 * ã¹ãããã®ãªã¼ã«å転é度(å®è¡æ¯ç§æ°)6 */7 var sec = 20;89 /*10 * ã¹ãããã®ãªã¼ã«æ
å ±11 * ã»ã¹ãããã®ãªã¼ã«element12 * ã»ã¹ãããã®ãªã¼ã«åæ¢ãã©ã°13 * ã»ã¹ãããã®ãªã¼ã«å転æ°14 */15 var $reels = [],16 stopReelFlag = [],17 reelCounts = [];1819 /*20 * ä½ç½®æ
å ±21 */22 var slotFrameHeight = 0,23 slotReelsHeight = 0,24 slotReelItemHeight = 0,25 slotReelStart = 0,26 slotReelStartHeight = 0;2728 /**29 * ã¹ããã30 */31 var Slot = {32 /**33 * åæåå¦ç34 */35 init: function init() {36 $reels[0] = $reels[1] = $reels[2] = null;37 stopReelFlag[0] = stopReelFlag[1] = stopReelFlag[2] = false;38 reelCounts[0] = reelCounts[1] = reelCounts[2] = 0;39 },40 /**41 * ã¹ã¿ã¼ããã¿ã³ã®ã¯ãªãã¯ã¤ãã³ã42 */43 start: function () {44 for (var index = 0; index<3; index++) {45 Slot.animation(index);46 }47 },48 /**49 * ã¹ããããã¿ã³ã®ã¯ãªãã¯ã¤ãã³ã50 */51 stop: function (index) {52 stopReelFlag[index] = true;53 if (stopReelFlag[0] && stopReelFlag[1] && stopReelFlag[2]) {54 // å
¨ãªã¼ã«åæ¢ããããªã»ãããã¿ã³ãæ¼ä¸ã§ããããã«ãã55 switch(reelCounts[0]){56 case 0:57 if(reelCounts[1] == 6 && reelCounts[2] == 7){58 //ããã«å½ããå¦ç59 $("#slot-frame").css( { 'background-color' : 'red' });60 console.log('reelCounts[0]', reelCounts[0]);61 console.log('reelCounts[1]', reelCounts[1]);62 console.log('reelCounts[2]', reelCounts[2]);63 value = new Uint8Array([0x04]);64 beepCharacteristic.writeValue(value); 65 }66 break;67 case 1:68 if((reelCounts[1] == 3 || reelCounts[1] == 12) && reelCounts[2] == 8){69 $("#slot-frame").css( { 'background-color' : 'red' });70 console.log('reelCounts[1]', reelCounts[1]);71 console.log('reelCounts[2]', reelCounts[2]);72 value = new Uint8Array([0x04]);73 beepCharacteristic.writeValue(value); 74 }75 break;76 case 2:77 if((reelCounts[1] == 7 || reelCounts[1] == 14) && (reelCounts[2] == 1 || reelCounts[2] == 9)){78 $("#slot-frame").css( { 'background-color' : 'red' });79 console.log('reelCounts[1]', reelCounts[1]);80 console.log('reelCounts[2]', reelCounts[2]);81 value = new Uint8Array([0x04]);82 beepCharacteristic.writeValue(value); 83 }84 break;85 case 3:86 if((reelCounts[1] == 2 || reelCounts[1] == 8 || reelCounts[1] == 11) && reelCounts[2] == 10){87 $("#slot-frame").css( { 'background-color' : 'red' });88 console.log('reelCounts[1]', reelCounts[1]);89 console.log('reelCounts[2]', reelCounts[2]);90 value = new Uint8Array([0x04]);91 beepCharacteristic.writeValue(value); 92 }93 break;94 case 4:95 if((reelCounts[1] == 0 || reelCounts[1] == 9) && (reelCounts[2] == 3 || reelCounts[2] == 5 || reelCounts[2] == 14)){96 $("#slot-frame").css( { 'background-color' : 'red' });97 console.log('reelCounts[1]', reelCounts[1]);98 console.log('reelCounts[2]', reelCounts[2]);99 value = new Uint8Array([0x04]);100 beepCharacteristic.writeValue(value); 101 }102 break;103 case 5:104 if((reelCounts[1] == 4 || reelCounts[1] == 13) && (reelCounts[2] == 2 || reelCounts[2] == 4 || reelCounts[2] == 13)){105 $("#slot-frame").css( { 'background-color' : 'red' });106 console.log('reelCounts[1]', reelCounts[1]);107 console.log('reelCounts[2]', reelCounts[2]);108 value = new Uint8Array([0x04]);109 beepCharacteristic.writeValue(value); 110 }111 break;112 case 6:113 if((reelCounts[1] == 1 || reelCounts[1] == 10) && (reelCounts[2] == 0 || reelCounts[2] == 11)){114 $("#slot-frame").css( { 'background-color' : 'red' });115 console.log('reelCounts[1]', reelCounts[1]);116 console.log('reelCounts[2]', reelCounts[2]);117 value = new Uint8Array([0x04]);118 beepCharacteristic.writeValue(value); 119 }120 break; 121 case 7:122 if(reelCounts[1] == 5 && reelCounts[2] == 6){123 $("#slot-frame").css( { 'background-color' : 'red' });124 console.log('reelCounts[1]', reelCounts[1]);125 console.log('reelCounts[2]', reelCounts[2]);126 value = new Uint8Array([0x04]);127 beepCharacteristic.writeValue(value); 128 }129 break; 130 case 8:131 if(reelCounts[1] == 6 && reelCounts[2] == 7){132 $("#slot-frame").css( { 'background-color' : 'red' });133 console.log('reelCounts[1]', reelCounts[1]);134 console.log('reelCounts[2]', reelCounts[2]);135 value = new Uint8Array([0x04]);136 beepCharacteristic.writeValue(value); 137 }138 break;139 case 9:140 if((reelCounts[1] == 3 || reelCounts[1] == 12) && reelCounts[2] == 8){141 $("#slot-frame").css( { 'background-color' : 'red' });142 console.log('reelCounts[1]', reelCounts[1]);143 console.log('reelCounts[2]', reelCounts[2]);144 value = new Uint8Array([0x04]);145 beepCharacteristic.writeValue(value); 146 }147 break;148 case 10:149 if((reelCounts[1] == 7 || reelCounts[1] == 14) && (reelCounts[2] == 1 || reelCounts[2] == 9)){150 $("#slot-frame").css( { 'background-color' : 'red' });151 console.log('reelCounts[1]', reelCounts[1]);152 console.log('reelCounts[2]', reelCounts[2]);153 value = new Uint8Array([0x04]);154 beepCharacteristic.writeValue(value); 155 }156 break;157 case 11:158 if((reelCounts[1] == 2 || reelCounts[1] == 8 || reelCounts[1] == 11) && reelCounts[2] == 10){159 $("#slot-frame").css( { 'background-color' : 'red' });160 console.log('reelCounts[1]', reelCounts[1]);161 console.log('reelCounts[2]', reelCounts[2]);162 value = new Uint8Array([0x04]);163 beepCharacteristic.writeValue(value); 164 }165 break;166 case 12:167 if((reelCounts[1] == 0 || reelCounts[1] == 9) && (reelCounts[2] == 3 || reelCounts[2] == 5 || reelCounts[2] == 14)){168 $("#slot-frame").css( { 'background-color' : 'red' });169 console.log('reelCounts[1]', reelCounts[1]);170 console.log('reelCounts[2]', reelCounts[2]);171 value = new Uint8Array([0x04]);172 beepCharacteristic.writeValue(value); 173 }174 break;175 case 13:176 if((reelCounts[1] == 4 || reelCounts[1] == 13) && (reelCounts[2] == 2 || reelCounts[2] == 4 || reelCounts[2] == 13)){177 $("#slot-frame").css( { 'background-color' : 'red' });178 console.log('reelCounts[1]', reelCounts[1]);179 console.log('reelCounts[2]', reelCounts[2]);180 value = new Uint8Array([0x04]);181 beepCharacteristic.writeValue(value); 182 }183 break;184 case 14:185 if((reelCounts[1] == 1 || reelCounts[1] == 10) && (reelCounts[2] == 0 || reelCounts[2] == 11)){186 $("#slot-frame").css( { 'background-color' : 'red' });187 console.log('reelCounts[0]', reelCounts[0]);188 console.log('reelCounts[1]', reelCounts[1]);189 console.log('reelCounts[2]', reelCounts[2]);190 value = new Uint8Array([0x04]);191 beepCharacteristic.writeValue(value); 192 }193 break; 194 }195 $('.btn-reset').attr('disabled', false);196 }197 },198 /**199 * ä½ç½®æ
å ±ã®åæåå¦ç200 */201 resetLocationInfo: function () {202 slotFrameHeight = $('.slot-frame').outerHeight();203 slotReelsHeight = $('.reels').eq(0).outerHeight();204 slotReelItemHeight = $('.reel').eq(0).outerHeight();205 slotReelStart = 5 - 2;206 // ãªã¼ã«ã®ä¸ä¸ã¯ãååã ã表示ãããããã®ä½ç½®èª¿æ´207 slotReelStartHeight = -slotReelsHeight;208 slotReelStartHeight = slotReelStartHeight + slotFrameHeight + ((slotReelItemHeight * 3 / 2) - (slotFrameHeight / 2));209210 $('.reels').css({211 'top':slotReelStartHeight 212 });213 },214 /**215 * ã¹ãããã®å転ã¢ãã¡ã¼ã·ã§ã³216 * reelCounts[index] = ãªã¼ã«ä¸é217 * index = ãªã¼ã«å218 */219 animation: function (index) {220 console.log('ã¢ãã¡ã¼ã·ã§ã³', 'éå§', index);221 if (reelCounts[index] >= 15) {222 reelCounts[index] = 0;223 }224 225 console.log('slotReelStartHeight', slotReelStartHeight);226 console.log('reelCounts[index]', reelCounts[index]);227 console.log('slotReelsHeight', slotReelsHeight);228 console.log('top', slotReelStartHeight + (reelCounts[index] * slotReelItemHeight));229230 /**231 * eqé¢æ°ã§æå®ã®reelãindexãåå¾232 * animateé¢æ°ã§åä½ã決å®233 * animate ãªãã¡ã¬ã³ã¹ : http://semooh.jp/jquery/api/effects/animate/params%2C+options/234 */235 $('.reels').eq(index).animate({236 'top': slotReelStartHeight + (reelCounts[index] * slotReelItemHeight)237 }, {238 duration: sec,239 easing: 'linear',240 complete: function () {241 console.log('ã¢ãã¡ã¼ã·ã§ã³', 'å®äº', index, reelCounts[index]);242 if (stopReelFlag[index]) {243 console.log('ã¢ãã¡ã¼ã·ã§ã³', 'ã¹ããã-', index, reelCounts[index]);244 return ;245 }246 // 移åéæ°ãã«ã¦ã³ã247 reelCounts[index]++;248 // ã¹ãããå転ã®ã¢ãã¡ã¼ã·ã§ã³ãå®è¡ãã249 Slot.animation(index);250 }251 });252 },253 };254255 global.Slot = Slot;256257})((this || 0).self || global);258259/**260 * èªã¿è¾¼ã¿å¾261 */262$(document).ready(function () {263264 /*265 * ã¹ãããã®åæåå¦çãå®è¡266 */267 Slot.init();268 Slot.resetLocationInfo();269270 /**271 * ã¹ã¿ã¼ããã¿ã³ã®ã¯ãªãã¯ã¤ãã³ã272 */273 document.body.addEventListener('keydown',274 event => {275 if (event.key === 'b') {276 Slot.start();277 $('.btn-stop').attr('disabled', false);278 }279 });280281 /**282 * ãªã»ãããã¿ã³ã®ã¯ãªãã¯ã¤ãã³ã283 */284 $('.btn-reset').click(function () {285 // ãªã»ãããã¿ã³ãæ¼ããªãããã«ãã286 $(this).attr('disabled', true);287 // ã¹ã¿ã¼ããã¿ã³ãæ¼ããããã«ãã288 $('.btn-start').attr('disabled', false);289 // ã¹ããããã¿ã³ãæ¼ããªãããã«ãã290 $('.btn-stop').attr('disabled', true);291 // ã¹ãããã®ãªã¼ã«æ
å ±ãåæå292 Slot.init();293 $("#slot-frame").css( { 'background-color' : 'white'});294 });295296 /**297 * ã¹ããããã¿ã³ã®ã¯ãªãã¯ã¤ãã³ã298 */299 $('.btn-stop').click(function () {300 // ã¹ããããã¿ã³ãæ¼ããªãããã«ãã301 $(this).attr('disabled', true);302 // ã¬ã¼ã«ã®å転ãåæ¢303 Slot.stop($(this).attr('data-val'));304 });305306});
...
monitoring.js
Source:monitoring.js
1var _ = require('lodash');2// Removes old monitoring data. We only want basic monitoring with the last 100 events.3// We keep last 80 and remove the rest to be sure.4function serverMonitoringCleanup(db, conn){5 var exclude = {6 eventDate: 0,7 pid: 0,8 version: 0,9 uptime: 0,10 network: 0,11 connectionName: 0,12 connections: 0,13 memory: 0,14 dataRetrieved: 0,15 docCounts: 016 };17 var retainedRecords = (24 * 60) * 60 / 30; // 24 hours worth of 30 sec blocks (data refresh interval)18 db.find({connectionName: conn}).skip(retainedRecords).sort({eventDate: -1}).projection(exclude).exec(function (err, serverEvents){19 var idArray = [];20 _.each(serverEvents, function(value, key){21 idArray.push(value._id);22 });23 db.remove({'_id': {'$in': idArray}}, {multi: true}, function (err, newDoc){});24 });25};26// runs a regular job against the connections and inserts into a local DB27var currDocCounts = {28 queried: 0,29 inserted: 0,30 deleted: 0,31 updated: 032};33exports.serverMonitoring = function (monitoringDB, dbs){34 if(dbs){35 Object.keys(dbs).forEach(function(key){36 var adminDb = dbs[key].native.admin();37 adminDb.serverStatus(function(err, info){38 // if we got data back from db. If not, normally related to permissions39 var dataRetrieved = false;40 if(info){41 dataRetrieved = true;42 }43 // doc numbers. We get the last interval number and subtract the current to get the diff44 var docCounts = '';45 var activeClients = '';46 var pid = 'N/A';47 var version = 'N/A';48 var uptime = 'N/A';49 var connections = '';50 var memory = '';51 // set the values if we can get them52 if(info){53 docCounts = info.metrics ? getDocCounts(currDocCounts, info.metrics.document) : 0;54 activeClients = info.globalLock ? info.globalLock.activeClients : 0;55 pid = info.pid;56 version = info.version;57 uptime = info.uptime;58 connections = info.connections;59 memory = info.mem;60 }61 var doc = {62 eventDate: new Date(),63 pid: pid,64 version: version,65 uptime: uptime,66 activeClients: activeClients,67 connectionName: key,68 connections: connections,69 memory: memory,70 dataRetrieved: dataRetrieved,71 docCounts: docCounts72 };73 // insert the data into local DB74 monitoringDB.insert(doc, function (err, newDoc){});75 // clean up old docs76 serverMonitoringCleanup(monitoringDB, key);77 });78 });79 }80};81function getDocCounts(currCounts, newCounts){82 var newDocCounts = {83 queried: 0,84 inserted: 0,85 deleted: 0,86 updated: 087 };88 // queried89 if(currCounts.queried === 0){90 currCounts.queried = newCounts.returned;91 }else{92 newDocCounts.queried = newCounts.returned - currCounts.queried;93 currCounts.queried = newCounts.returned;94 }95 // inserts96 if(currCounts.inserted === 0){97 currCounts.inserted = newCounts.inserted;98 }else{99 newDocCounts.inserted = newCounts.inserted - currCounts.inserted;100 currCounts.inserted = newCounts.inserted;101 }102 // deleted103 if(currCounts.deleted === 0){104 currCounts.deleted = newCounts.deleted;105 }else{106 newDocCounts.deleted = newCounts.deleted - currCounts.deleted;107 currCounts.deleted = newCounts.deleted;108 }109 // updated110 if(currCounts.updated === 0){111 currCounts.updated = newCounts.updated;112 }else{113 newDocCounts.updated = newCounts.updated - currCounts.updated;114 currCounts.updated = newCounts.updated;115 }116 return newDocCounts;...
WebmentionCount.js
Source:WebmentionCount.js
1import React, { useEffect, useState } from 'react'2const initialCounts = {3 count: 0,4 type: {5 like: 0,6 mention: 0,7 reply: 0,8 repost: 0,9 },10}11const WebmentionCount = ({ target }) => {12 const [counts, setCounts] = useState(initialCounts)13 // Get counts on `target` change.14 useEffect(() => {15 async function getCounts() {16 const url = `https://webmention.io/api/count.json?target=${target}`17 const responseCounts = await fetch(url).then(response => response.json())18 setCounts(previousCounts => {19 return {20 ...previousCounts,21 ...responseCounts,22 type: {23 ...previousCounts.type,24 ...responseCounts.type,25 },26 }27 })28 }29 getCounts()30 }, [target])31 return (32 <>33 {counts && (34 <>35 <span role="img" aria-label="emoji">36 ð37 </span>{' '}38 {counts.type.like + counts.type.repost || 0}{' '}39 <span role="img" aria-label="emoji">40 ð¬41 </span>{' '}42 {counts.type.mention + counts.type.reply || 0}43 </>44 )}45 </>46 )47}...
Using AI Code Generation
1var wpt = require('wpt');2var options = {3};4wpt.runTest('API_KEY', options, function(err, data) {5 if (err) {6 console.log('Error: ' + err.message);7 } else {8 console.log('Test Status: ' + data.statusCode);9 console.log('Test ID: ' + data.data.testId);10 console.log('Test Results Available In: ' + data.data.userUrl);11 }12});
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Barack Obama');3page.get(function(err, resp) {4 console.log(resp);5});6var wptools = require('wptools');7var page = wptools.page('Barack Obama');8page.get(function(err, resp) {9 console.log(resp);10});11var wptools = require('wptools');12var page = wptools.page('Barack Obama');13page.get(function(err, resp) {14 console.log(resp);15});16var wptools = require('wptools');17var page = wptools.page('Barack Obama');18page.get(function(err, resp) {19 console.log(resp);20});21var wptools = require('wptools');22var page = wptools.page('Barack Obama');23page.get(function(err, resp) {24 console.log(resp);25});26var wptools = require('wptools');27var page = wptools.page('Barack Obama');28page.get(function(err, resp) {29 console.log(resp);30});31var wptools = require('wptools');32var page = wptools.page('Barack Obama');33page.get(function(err, resp) {34 console.log(resp);35});36var wptools = require('wptools');37var page = wptools.page('Barack Obama');38page.get(function(err, resp) {39 console.log(resp);40});41var wptools = require('wptools');42var page = wptools.page('Barack Obama');43page.get(function(err, resp) {44 console.log(resp);45});46var wptools = require('wptools');
Using AI Code Generation
1var wptools = require('wptools');2wptools.counts('Barack Obama').then(function(response) {3 console.log(response);4});5 wptools.counts('Barack Obama').then(function(response) {6 console.log(response);7 });8{ views: 1265, edits: 212, pages: 1, files: 1, users: 1, categories: 1 }9wptools.counts('Barack Obama').then(function(response) {10 console.log(response);11});12{ views: 1265, edits: 212, pages: 1, files: 1, users: 1, categories: 1 }13{ lang: 'en', project: 'wikipedia', pageid: '' }14wptools.counts('Barack Obama', function(error, response) {15 if (error) {16 console.log(error);17 } else {18 console.log(response);19 }20});21{ views: 1265, edits: 212, pages: 1, files: 1, users: 1, categories: 1 }22{ lang: 'en', project: 'wikipedia', pageid: '' }
Using AI Code Generation
1var wptools = require('./wptools.js').wptools;2wptools.page(url).then(function(page) {3 return page.counts();4}).then(function(counts) {5 console.log(counts);6});7var wptools = function() {8 var page = function(url) {9 return {10 counts: function() {11 return new Promise(function(resolve, reject) {12 resolve({count1: 2, count2: 3});13 });14 }15 }16 };17 return {18 };19}();20module.exports.wptools = wptools;
Using AI Code Generation
1const wptools = require('wptools');2const wpt = wptools.page('Barack Obama');3wpt.counts()4.then(function(result) {5 console.log(result);6})7.catch(function(err) {8 console.log(err);9});10{ pages: 0,
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Barack Obama');3page.counts(function(err, resp){4 console.log(resp);5});6{ 7 { edit: 1031,8 import: 0,
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools.page('Albert Einstein');3wp.get(function(err, resp){4 console.log(resp.data.counts);5});6{ edits: 43187,7 templates_created: 0 }8var wptools = require('wptools');9var wp = wptools.page('Albert Einstein');10wp.get(function(err, resp){11 console.log(resp.data.counts);12});13{ edits: 43187,14 templates_created: 0 }15var wptools = require('wptools');16var wp = wptools.page('Albert Einstein');17wp.get(function(err, resp){18 console.log(resp.data.counts);19});20{ edits: 43187,21 templates_created: 0 }
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.2d1b8f1b1c0b7e0d0f8a8e0b0c9e9d1e');3 if (err) return console.error(err);4 console.log('Test submitted. Polling for results...');5 wpt.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 });9});10 if (err) return console.error(err);11 console.log('Test submitted. Polling for results...');12 wpt.getTestResults(data.data.testId, function(err, data) {13 if (err) return console.error(err);14 console.log(data);15 });16});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!